Asp.net - Web.Config - Custom Errors

Riz picture Riz · Aug 4, 2011 · Viewed 12.6k times · Source

How can I set 404 and other error pages using web.config? I have tried adding following block in web.config.

     <customErrors defaultRedirect="Forms/Errors/Page_404.aspx" mode="On">
    <error statusCode="500" redirect="servererror.aspx" />
        <error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="Forms/Errors/Page_404.aspx" />
    </customErrors>

but still its showing default error page of IIS7. How to fix this?

Answer

Riz picture Riz · Aug 19, 2011

I solved it myself. We need to add another section in web.config like below to make it work in IIS 7 / 7.5. For IIS 6 the one works which I mentioned in my question

<system.webServer>
...
<httpErrors errorMode="Custom" >
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/404.aspx" responseMode="Redirect" />
<error statusCode="403" path="/403.aspx" responseMode="Redirect" />
<error statusCode="500" path="/500.aspx" responseMode="Redirect" />         
</httpErrors>
...
</system.webServer>

Thanks to everyone who answered.