2

I am trying to figure out why I am getting a NullReferenceException with the following code. Everything seems to look like it is nested properly. I am using Microsoft.Web.Administration and I am attempting to modify the prefixLanguageFilePath for all the errors. Any help would be appreciated!

    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>

Edit:

I got it working by looping through the collection without denoting the error tag and just looking for the attribute. I will have to come back to it to see if there would be any unintended adverse "features" by doing it this way.

var httpErrorsCollection = config.GetSection("system.webServer/httpErrors")
       .GetCollection(); 

foreach (var error in httpErrorsCollection) 
    error.Attributes["prefixLanguageFilePath"].Value = dir;
H H
  • 263,252
  • 30
  • 330
  • 514
  • Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. In particular, break that long expression apart and see which part is null. – John Saunders Sep 06 '12 at 20:08

1 Answers1

0
//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;

etc

H H
  • 263,252
  • 30
  • 330
  • 514
  • Henk, thanks but I cannot debug this as I do not have IIS installed locally and I do not have admin rights to install debugging remotely on the IIS server. The only feedback I am getting is the catch exception. – timduhenchanter Sep 06 '12 at 20:25
  • Ok, so I validated "errorSection" however "errors" is uninitialized. So now based on looking at the XML, how is that possible? – timduhenchanter Sep 06 '12 at 20:53
  • Not sure - there are multiple candidates, the return can only be 1. Or `null`. – H H Sep 06 '12 at 21:12
  • I got it working by looping through the collection without denoting the error tag and just looking for the attribute. I will have to come back to it to see if there would be any unintended adverse "features" by doing it this way. – timduhenchanter Sep 06 '12 at 21:18
  • ` var httpErrorsCollection = config.GetSection("system.webServer/httpErrors").GetCollection(); foreach (var error in httpErrorsCollection) error.Attributes["prefixLanguageFilePath"].Value = dir;` – timduhenchanter Sep 06 '12 at 21:19
  • OK, Note that you can _edit_ your question. Relevant info should not be in comments. – H H Sep 06 '12 at 21:23