我试图弄清楚为什么我用下面的代码得到一个NullReferenceException
。一切似乎都正确嵌套。我正在使用Microsoft.Web.Administration
,并且尝试为所有错误修改prefixLanguageFilePath。任何帮助,将不胜感激!
public static void ModifyCustomErrDir(string dir)
{
try
{
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
config.GetSection("system.webServer/httpErrors").ChildElements["error"].Attributes["prefixLanguageFilePath"].Value = dir;
}
catch (ServerManagerException e)
{
Console.WriteLine(e);
}
}
<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
<error statusCode="401" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="401.htm" />
<error statusCode="403" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="403.htm" />
<error statusCode="404" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="404.htm" />
<error statusCode="405" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="405.htm" />
<error statusCode="406" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="406.htm" />
<error statusCode="412" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="412.htm" />
<error statusCode="500" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="500.htm" />
<error statusCode="501" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="501.htm" />
<error statusCode="502" prefixLanguageFilePath="%SystemDrive%\inetpub\custerr" path="502.htm" />
</httpErrors>
编辑:
我通过遍历集合而不表示错误标签而只寻找属性来使其工作。我将不得不回到它的角度,看看这样做是否会有任何意想不到的不利“功能”。
var httpErrorsCollection = config.GetSection("system.webServer/httpErrors")
.GetCollection();
foreach (var error in httpErrorsCollection)
error.Attributes["prefixLanguageFilePath"].Value = dir;
最佳答案
//config.GetSection("system.webServer/httpErrors").ChildElements["error"].
// Attributes["prefixLanguageFilePath"].Value = dir;
var errorSection = config.GetSection("system.webServer/httpErrors");
// check errorSection
var errors = errorSection.ChildElements["error"];
// check errors
errors.Attributes["prefixLanguageFilePath"].Value = dir;
等等
关于c# - Microsoft.Web.Administration-ApplicationHost.Config中带有httpError的NullReferenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12307576/