gpt4 book ai didi

c# - Microsoft.Web.Administration - NullReferenceException 与 ApplicationHost.Config 中的 httpErrors

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:54 24 4
gpt4 key购买 nike

我试图弄清楚为什么我使用以下代码得到 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 - NullReferenceException 与 ApplicationHost.Config 中的 httpErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com