Fallback from dynamic error page to static error page - c#

I know, we should always use static error pages, but in my case I've got to use a dynamic error page "ErrorPage.aspx" with a .cs file. Now since we are running a security audit for https certificate; it shows a very possible scenario where there might be an error in this ErrorPage.aspx itself.
I've specified the custom error element in the web config which redirects any error to this dynamic page and as usual have set the mode to on.
e.g.
<customErrors mode="On" defaultRedirect="ErrorPage.aspx" >
<error statusCode="404" redirect="Login.aspx"/>
</customErrors>
Now what if there's an error in error page, where shall I redirect it. Therefore, in my first attempt, I added a static html page error.htm to the project and in my errorpage.aspx's page directive I added errorpage="error.htm". But it's not working, instead it shows me a web config error saying I should set mode to "RemoteOnly".
I think I need to check explicitly the error codes in errorpage.aspx and then perform the re-direction but that'll be quite lengthy. I want to know if there's a better option to do it.
I hope I've made my problem clear.

Related

ASP.NET display "site down" page instead of YSOD if failure during startup

I'm trying to serve a custom "site down" page instead of the standard YSOD if the server were ever to fail during it's startup. Using <customErrors> inside of web.Config work's fine for any server error occurring after startup has completed successfully, but in this case the YSOD response is not replaced.
Other resources have lead me to adding app_offline.htm to the root of the project and playing with its name during deployment, but this does not tackle unexpected server issues occurring during startup.
Is there a way to serve a "default" page if there are any errors or a proper HTTP response could not be formed?
Yes there is a way to do that but that depends how you want to handle those errors.
If you want to redirect to the same error page on any 4xx or 5xx status codes then you can use the below code in your web.config file
<customErrors mode="On" defaultRedirect="Error.aspx">
</customErrors>
In error page you can display some user friendly message.

Write and display error message for live website?

I am developing a website, At the moment when i run it off my local host every thing works fine.
I have uploaded this website to fireFTP and can now access my website online.
The website does not preform the same as when hosted on local host and i cant find the errors as my website does not break.
I would like to do some testing and am wanting to know how to display an error message??
I would like to display the error message like this:
But with a custom message like, "code stepped into method writeToSpreadSheet".
This way i could find where my code gets to and pin point whats going wrong.
You need to enable show detailed error messages in IIS settings, see the link below for a short tutorial about how to do it.
IIS7 : HOW TO enable the detailed error messages for the website while browsed from for the client browsers?
Add this lines in web.config inside so you dont have
to touch the IIS configuration:
<customErrors mode="On">
<error redirect="error404.html" statusCode="404" />
</customErrors>
Change the error404.html for your desired html page.
When some page is not found user will be redirected there

How to change the default behaviour for customErrors pages in Asp.Net

I use Asp.Net 4 and C#.
I would like to know how to change the default beahviour of Asp.Net customErrors.
In my Web.Config file I use this settings.
<customErrors mode="On" defaultRedirect="/ErrorPages/Oops.aspx">
<error statusCode="404" redirect="/ErrorPages/404.aspx" />
</customErrors>
As you can expect if a page is not found a redirect occur to the custom page specified in Web.Config.
Requested page:
http://localhost:1372/nopagehere
Result page:
http://localhost:1372/ErrorPages/404.aspx?aspxerrorpath=/nopagehere
When I analyse the Http Header for the Result page I can see:
a 404 Status when the page is not found
a 302 Status for the new created url
I would like change this behavior in this way:
when a request to a not found page has been made http://localhost:1372/nopagehere
the result should be only http://localhost:1372/nopagehere (no 302 redirect) and with status code only on 404.
Any idea how to do it? Thanks for your time on this.
I guess you are looking for the redirectMode attribute on your <customErrors> section.
See: http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx
I think you want to set it to redirectMode="ResponseRewrite". This throws a 404 and shows the content of your 404 page, but without redirecting to the actual 404 page.
Maybe removing the status code 404 from the custom errors and let the default redirect page handle it.

Problem with defaultRedirect in Web.Config <customErrors>

I use Asp.net 4.
Here setting for my Web.Config file.
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ErrorPages/Oops.aspx">
<error statusCode="404" redirect="/ErrorPages/404.aspx" />
</customErrors>
I need to apply the defaultRedirect url for any Exception, and use the redirectMode="ResponseRewrite" for only Page Not Found 404.
With this setting at the moment I can manage properly the Page Not Found (I'm able to get the 404.aspx page) but if an exception arise on any other page I get the "Internet Explorer cannot display the webpage" and I'm not able to reach the defaultRedirect Oops.aspx.
What I'm doing here wrong?
One problem with having an aspx page as the target of a custom errors redirect is that errors that aren't specific to a page (e.g. errors in global.asax; errors processing web.config) will also be thrown by the target page, which can result in an infinite redirect loop.
For this reason, it's often better to have a static html page as the target of at least the defaultRedirect.
Try putting the ~ in the redirect URL.
redirect="~/ErrorPages/404.aspx"
How many bytes is the error page content and does it set an HTTP status code other than 200 OK? In Internet Explorer with "Show friendly HTTP error messages" on, which is the default, custom error pages that have content under 512 bytes in length will be suppressed by the IE browser and replaced with the "friendly" error message.
To see if this is your problem, you can try turning off the "Show friendly HTTP error messages" option under Tools, Internet Options, Advanced, Settings. It'll be under the Browsing category in the Settings area.
If the page shows up with the option turned off, try turning it back on and changing your page to add an image or some other element that will make the size over 512 bytes long.

HttpRequestValidationException showing YSOD despite customErrors section

we've modified our customErrors section to protect against the recent ASP.NET vulnerability.
Our problem is, that HttpRequestValidationException's are now causing a YSOD's to be shown, other exceptions and page not found errors are showing our custom error page.
If we change the redirectMode to ResponseRedirect everything works fine.
This is our modified section:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error.aspx"/>
This question has nothing to do with the recent ASP.NET vulnerability!It's about the ResponseRewrite redirectMode in combination with HttpRequestValidationException's. We know there will soon be a patch and that we could change back to ResponseRedirect.
Kind regards, Martin
This happens when your error page generates an error.
Make sure you turn off request validation on your error page. <%# Page ValidateRequest="false" %>
You then of course need to encode any user input that displays on your error page. eg. Server.HtmlEncode(ex.Message)

Categories