Asp.net – Web.Config – Custom Errors

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Try putting this in the system.webServer section of your Web.config

<system.webServer>
  <httpErrors existingResponse="PassThrough" />
</system.webServer>

Method 3

Try to add the “~/” before paths:

 <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>

Method 4

It looks like you’re using a relative path there. Could that be the problem?

Try using Fiddler to see what page your browser is being redirected to.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x