Search code examples
asp.netwebformsweb-configweb.config-transform

How can we add httpErrors section in web..config from a seprate config file


I have a web.config file and adding

<httpErrors existingResponse="Replace" errorMode="Custom" >
            <remove statusCode="404" subStatusCode="-1"/>
            <error statusCode="404" prefixLanguageFilePath="" path="{ApplicationPath}/Error/PageNotFound.aspx" responseMode="ExecuteURL" />
            <remove statusCode="403" subStatusCode="-1"/>
            <error statusCode="403" prefixLanguageFilePath="" path="{ApplicationPath}/Error/AccessDenied.aspx" responseMode="ExecuteURL" />
            <remove statusCode="500" subStatusCode="-1"/>
            <error statusCode="500" prefixLanguageFilePath="" path="{ApplicationPath}/Error/GenericError.aspx" responseMode="ExecuteURL" />
        </httpErrors>

I want to get out this part from main webconfig and link it from another file. So that each server (local or server) can have its own applciationPath. My question is how can we add sub config file to override main config just the httpError part. My problem can be solved if we could access Request variables like {ApplicationPath} in web config too. On my local the website is hosted in a sub folder ( hosting multiple sites in iis) on server it is on the root


Solution

  • You can use the configSource attribute. For example, in your web.config do this:

    <httpErrors configSource="httpErrors.config"/>
    

    And in a file called httpErrors.config:

    <?xml version="1.0"?>
    
    <httpErrors>
        <remove statusCode="404" subStatusCode="-1"/>
        <error statusCode="404" path="... etc..." responseMode="ExecuteURL" />
    </httpErrors>