Custom error handling for a specific page in ASP.Net - asp.net

On a site I run, I have 404's and 500 errors mapped to redirect to a custom error page for end users; using the following code in my web.config:
...
...
<system.web>
<customErrors defaultRedirect="/404/default.aspx" mode="RemoteOnly">
<error statusCode="404" redirect="/404/default.aspx" />
</customErrors>
...
...
However, I have one specific page that I do not want redirected; a health check page to make sure the site is 100% operational. I tried setting a location specific custom error handler using the code below in my web.config:
...
...
</system.web>
<location path="health.aspx">
<system.web>
<customErrors defaultRedirect="" mode="RemoteOnly"></customErrors>
</system.web>
</location>
...
...
However, it doesn't seem to work. When I rename health.aspx to something else like badhealth.aspx, then make a request, I expect to get a generic 404 error via the Yellow Screen of Death page. Similarly, by intentionally changing the code to throw an error, I should get 500 error via the YSOD page. In both cases, I end up being redirected to our custom 404 page, rathen then getting a YSOD. Thoughts?
Any assistance is greatly appreciated.
Thanks all,
- Frank

Nevermind, we solved the issue. It seems when you have a location specific customError directive; if you leave the defaultRedirect as "", it will default to the sitewide defaultRedirect URL.
The solution was simply to turn off CustomError handling for this specific path, like so:
<system.web>
<customErrors defaultRedirect="/404/default.aspx" mode="RemoteOnly">
<error statusCode="404" redirect="/404/default.aspx" />
</customErrors>
</system.web>
<location path="health.aspx">
<system.web>
<customErrors mode="Off"/>
</system.web>
</location>
Thanks anyway, Jose.

Related

DefaultRedirect not working on mvc 4 web.config

Hi there fellow programmers! I have a problem, I am facing a fancy problem of redirecting to a custom error page. Here is what i have in my web.config
<system.web>
<customErrors mode="On" defaultRedirect="/Message/DefaultError" redirectMode="ResponseRedirect" >
<error statusCode="404" redirect="/NotFound" />
<error statusCode="500" redirect="/ServerError"/>
</customErrors>
</system.web>
on error 404 and 500 which were declared above were working fine, but my main problem is working with not enumerated errors such ass 400 etc.. i want to catch ALL OTHER ERROR on the default redirect action. or is that possible??? Help please????
i want to catch ALL OTHER ERROR on the default redirect action
Since 'All other errors' (400, 401 etc) will most likely be considered a Server-Error and not an Application-Error, you need to set the default page on the <system.webServer> section:
<system.webServer>
<httpErrors defaultPath="/Message/DefaultError" defaultResponseMode="Redirect">
<clear />
</httpErrors>
</system.webServer>
See Documentation

invalid or unknown .aspx pages do not trigger 404, and instead display marker file text

On my web server, if i visit a page that does not exist, it generates a proper 404, UNLESS that page ends in .aspx. If the invalid url ends with .aspx, IIS generates the marker file error:
"This is a marker file generated by the precompilation tool, and should not be deleted!"
instead of a proper 404. How can i make IIS display/serve a 404 for unknown or invalid .aspx pages?
For example:
http://www.mysite.com/iDontExist.txt --> generates proper 404
http://www.mysite.com/iDontExist.aspx --> marker file error instead of 404
any ideas?
You need to turn on the custome errors via the web.config
<configuration>
<system.web>
<customErrors defaultRedirect="GenericError.html" mode="On">
<error statusCode="404" redirect="PageNotFound.html"/>
</customErrors>
</system.web>
</configuration>
More information on this can be found in MSDN
The trick was to set the "redirect mode" of the customErrors tag to 'ResponseRedirect' instead of 'ResponseRewrite':
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="customError.aspx" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="customError404.aspx" />
<error statusCode="500" redirect="customError500.aspx" />
</customErrors>
</system.web>

not found error not handling in web.config

i am trying to put customError tag in web.config file..
still it doesn't redirect to default url
http://www.lacopainn.com/page/2/?p=dbasndwefxknyafc&wpmp_switcher=mobile
here is my site. when we enter this url it shows error of
Not Found
The requested document was not found on this server.
how can i handle this error and redirect to detault page??
please help
you need to put following tag under System.web tag in your webconfig file
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="default.aspx" />
</customErrors>
</system.web>
Hope it will fix your problem.

ASP.Net partially ignoring my Custom error section in web.config

Here's my web.config customErrors section (you'll notice I've switched the mode to 'On' so I can see the redirect on my localhost):
<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On" redirectMode="ResponseRewrite">
<error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />
</customErrors>
and here's the code that throws:
Catch adEx As AccessDeniedException
Throw New HttpException(DirectCast(HttpStatusCode.Forbidden, Integer), adEx.Message)
End Try
and here's what I end up with:
Which is not my pretty AccessDenied.aspx page but it is a forbidden error page so at least I know my throw is working.
I've removed the entry for 403 in IIS (7.0) as a desperate last attempt and unsuprisingly that made no difference.
I've run out of ideas now so any suggestions will be gratefully appreciated!
In fact, your aspx page may not be executing at all.
Go to IIS.
Go to your default website properties.
Click on the Home Directory Tab
Click the configuration button.
Now, check if a .aspx page is registered there.
You need to specify existingResponse="PassThrough" in the system.webServer section of the httpErrors element.
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>

Special Error Page in Asp.Net

i want to redirect to a special page which shows detailed error message for unexpected errors.Which solution is the best for it in asp.net?Can u give examples? Thanks
It sounds like all you really want to do is turn off custom errors in your web.config file:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>
That should cause a full "yellow screen of death" to appear for any unhandled exception. You can also use this to set the behavior so that the full messages only shows up on your local machine, but your users are redirected to something... friendlier:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="MyErrorPage.aspx" />
</system.web>
</configuration>

Resources