jos*_*ley 9 .net iis-7 configurationelement servermanager
假设我有以下<httpErrors>集合web.config:
<httpErrors>
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
是的,很好'空.
在IIS 7中,我的HTTP错误页面如下所示:

美丽!(我突然强调了404,因为这是我将在一秒内使用的例子).
现在,我运行以下代码:
errorElement["statusCode"] = 404;
errorElement["subStatusCode"] = -1;
errorElement["path"] = "/404.html";
httpErrorsCollection.Add(errorElement);
Run Code Online (Sandbox Code Playgroud)
可爱.我现在按照预期在我的web.config:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/404.html" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
不能更快乐.现在,在IIS 7中,我的HTTP错误部分看起来像预期的那样,如下所示:

在这一点上,生活不能更甜蜜.现在,在线下,我想以编程方式将我的404错误恢复到原始屏幕截图中显示的状态.逻辑规定我应该remove犯我的新错误:
httpErrorsCollection.Remove(errorElement);
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做,我web.config看起来很像这样:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
我的IIS看起来有点像这样:

这是因为我的web.config- 但是如何,使用ServerManager和所有它有用的IIS 7 API,我是否httpError完全删除元素并将其恢复web.config为:
<httpErrors>
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
小智 19
在IIS7及以上的管理部分下,我们可以看到一个名为Configuration Editor的图标, 双击并展开到
system.webserver - >的WebDAV - > httpErrors
右键单击 " 默认路径 "下的
部分 - >单击"还原为父级"
然后重启网站
更改将被还原
我以前碰到过这个。我可以完成这项工作的唯一方法是在进行任何更改之前调用RevertToParent()该system.webServer/httpErrors部分并提交更改,例如:
ConfigurationSection httpErrorsSection =
config.GetSection("system.webServer/httpErrors");
// Save a copy of the errors collection first (see pastebin example)
httpErrorsCollectionLocal =
CopyLocalErrorCollection(httpErrorsSection.GetCollection()
.Where(e => e.IsLocallyStored)
.ToList<ConfigurationElement>());
httpErrorsSection.RevertToParent();
serverManager.CommitChanges();
Run Code Online (Sandbox Code Playgroud)
您必须在RevertToParent() is called because the errors collection remains intact until调用 CommitChanges()`后提交。
然后,您必须重新添加本地错误集合的已保存副本,在重新添加自定义错误时删除或更新自定义错误。
我在下面粘贴了一个完整的工作示例。该代码适用于站点根目录,web.config但稍加修改,您可以添加对子web.config文件夹中文件的支持:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
namespace IIS7_HttpErrorSectionClearing
{
class Program
{
static void Main(string[] args)
{
long siteId = 60001;
// Add some local custom errors
CustomErrorManager.SetCustomError(siteId, 404, -1, ResponseMode.File, @"D:\websites\60001\www\404.html");
CustomErrorManager.SetCustomError(siteId, 404, 1, ResponseMode.File, @"D:\websites\60001\www\404-1.html");
CustomErrorManager.SetCustomError(siteId, 404, 5, ResponseMode.File, @"D:\websites\60001\www\404-5.html");
// Change existing local custom error
CustomErrorManager.SetCustomError(siteId, 404, 1, ResponseMode.ExecuteURL, "/404-5.aspx");
// Revert to inherited
CustomErrorManager.RevertCustomError(siteId, 404, 5);
CustomErrorManager.RevertCustomError(siteId, 404, -1);
CustomErrorManager.RevertCustomError(siteId, 404, 1);
}
}
public enum ResponseMode
{
File,
ExecuteURL,
Redirect
}
public static class CustomErrorManager
{
public static void RevertCustomError(long siteId, int statusCode, int subStatusCode)
{
List<Error> httpErrorsCollectionLocal = CopyLocalsAndRevertToInherited(siteId);
int index = httpErrorsCollectionLocal.FindIndex(e => e.StatusCode == statusCode && e.SubStatusCode == subStatusCode);
if(index > -1)
{
httpErrorsCollectionLocal.RemoveAt(index);
}
PersistLocalCustomErrors(siteId, httpErrorsCollectionLocal);
}
public static void SetCustomError(long siteId, long statusCode, int subStatusCode,
ResponseMode responseMode, string path)
{
SetCustomError(siteId, statusCode, subStatusCode, responseMode, path, null);
}
public static void SetCustomError(long siteId, long statusCode, int subStatusCode,
ResponseMode responseMode, string path, string prefixLanguageFilePath)
{
List<Error> httpErrorsCollectionLocal = CopyLocalsAndRevertToInherited(siteId);
AddOrUpdateError(httpErrorsCollectionLocal, statusCode, subStatusCode, responseMode, path, prefixLanguageFilePath);
PersistLocalCustomErrors(siteId, httpErrorsCollectionLocal);
}
private static void PersistLocalCustomErrors(long siteId, List<Error> httpErrorsCollectionLocal)
{
using (ServerManager serverManager = new ServerManager())
{
Site site = serverManager.Sites.Where(s => s.Id == siteId).FirstOrDefault();
Configuration config = serverManager.GetWebConfiguration(site.Name);
ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();
foreach (var localError in httpErrorsCollectionLocal)
{
ConfigurationElement remove = httpErrorsCollection.CreateElement("remove");
remove["statusCode"] = localError.StatusCode;
remove["subStatusCode"] = localError.SubStatusCode;
httpErrorsCollection.Add(remove);
ConfigurationElement add = httpErrorsCollection.CreateElement("error");
add["statusCode"] = localError.StatusCode;
add["subStatusCode"] = localError.SubStatusCode;
add["responseMode"] = localError.ResponseMode;
add["path"] = localError.Path;
add["prefixLanguageFilePath"] = localError.prefixLanguageFilePath;
httpErrorsCollection.Add(add);
}
serverManager.CommitChanges();
}
}
private static List<Error> CopyLocalsAndRevertToInherited(long siteId)
{
List<Error> httpErrorsCollectionLocal;
using (ServerManager serverManager = new ServerManager())
{
Site site = serverManager.Sites.Where(s => s.Id == siteId).FirstOrDefault();
Configuration config = serverManager.GetWebConfiguration(site.Name);
ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();
// Take a copy because existing elements can't be modified.
httpErrorsCollectionLocal = CopyLocalErrorCollection(httpErrorsSection.GetCollection()
.Where(e => e.IsLocallyStored).ToList<ConfigurationElement>());
httpErrorsSection.RevertToParent();
// Have to commit here because RevertToParent won't clear the collection until called.
serverManager.CommitChanges();
return httpErrorsCollectionLocal;
}
}
private static List<Error> CopyLocalErrorCollection(List<ConfigurationElement> collection)
{
List<Error> errors = new List<Error>();
foreach (var error in collection)
{
errors.Add(new Error()
{
StatusCode = (long)error["statusCode"],
SubStatusCode = (int)error["subStatusCode"],
ResponseMode = (ResponseMode)error["responseMode"],
Path = (string)error["path"],
prefixLanguageFilePath = (string)error["prefixLanguageFilePath"]
});
}
return errors;
}
private static void AddOrUpdateError(List<Error> collection, long statusCode, int subStatusCode,
ResponseMode responseMode, string path, string prefixLanguageFilePath)
{
// Add or update error
Error error = collection.Find(ce => ce.StatusCode == statusCode && ce.SubStatusCode == subStatusCode);
if (error == null)
{
collection.Add(new Error()
{
StatusCode = statusCode,
SubStatusCode = subStatusCode,
ResponseMode = responseMode,
Path = path,
prefixLanguageFilePath = prefixLanguageFilePath
});
}
else
{
error.ResponseMode = responseMode;
error.Path = path;
error.prefixLanguageFilePath = prefixLanguageFilePath;
}
}
private class Error
{
public long StatusCode { get; set; }
public int SubStatusCode { get; set; }
public ResponseMode ResponseMode { get; set; }
public string Path { get; set; }
public string prefixLanguageFilePath { get; set; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10039 次 |
| 最近记录: |