I have the following configuration in my web.config:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/Error.html">
<error statusCode="404" redirect="~/Error/Error.html" />
<error statusCode="400" redirect="~/Error/Error.html" />
</customErrors>
FWIW, this is an ASP.NET MVC 3 application.
When I generate an error. For example by visiting..
http://testserver/this&is&an&illegal&request
.. which is blocked by ASP.NET request validation, the error page is returned, but there is no content-type header. IE infers the content and renders the HTML, however Firefix (correctly IMO) treats the content as text and displays the HTML code.
Are there additional steps that I need to take to persuade ASP.NET to send a content type header? I assume this is related to the fact that it's picking the files up from the file system, but the MIME types appear to be configured correctly on the server.
In ASP.Net, error can be handled programmatically by writing appropriate code in the page-level error event, for errors on an individual page or in the application-level error event for handling errors that may occur in any page of the application.
The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD. The mode attribute is required and accepts one of three values: On , Off , or RemoteOnly .
Custom error pages enable you to customize the pages that display when an error occurs. This makes your website appear more professional and also prevents visitors from leaving your site. If a visitor sees a generic error page, they are likely to leave your site.
My ASP.NET MVC 2 application was sending the content-type header - Content-Type: text/html - correctly. Then it started giving this weird problem after upgrading the application pool from .Net Framework v2 to .Net Framework v4. I'm using the following configuration
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/500.html">
<error statusCode="404" redirect="/404.html" />
</customErrors>
and I wanted to stick to static pages for my custom error pages.
The only solution I could think of was setting the header explicitly in the Application_Error method of the MvcApplication class in the Global.asax.cs file.
public class MvcApplication : System.Web.HttpApplication
{
// .
// .
// .
void Application_Error(object sender, EventArgs e)
{
// .
// Remember to set Response.StatusCode and
// Response.TrySkipIisCustomErrors
// .
Response.ContentType = "text/html";
// .
// .
// .
}
// .
// .
// .
}
A bit annoying but the simplest solution that came to my mind.
With a static file error page, I have not found any solution.
Using a dynamic error page, headers are correctly set. But you may lose the error status code in the process and should then set it yourself in the error page code. (That is somewhat the answer of rangitatanz, but maybe written more explicitly.)
So here is an example solution I use (webform way):
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/unknown.aspx">
<error statusCode="404" redirect="~/Error/404.aspx" />
<error statusCode="400" redirect="~/Error/400.aspx" />
</customErrors>
And in page load (here for the 404 one):
protected void Page_Load(object sender, EventArgs e)
{
Response.StatusCode = 404;
Response.StatusDescription = "Not found";
Response.TrySkipIisCustomErrors = true;
}
This could be used in a MVC project. If you do not want to mix webforms and MVC, you could write an error controller for that with its associated views. (That is indeed the way I go in my MVC project, but currently I am back on a webform one and so I have re-solved that using webform.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With