简体   繁体   中英

ASP.NET rewritten custom errors do not send content-type header

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.

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

ASP.Net wont' be sending this IIS will. If you want ASP.Net to send it then try add an Error.aspx and see what that looks like?

What I mean here is that you will be sent to an html page - which unless you have wired up IIS to run these through aspnet then it will just be served up normally.

If you add a new page error.aspx you can a) check if ASP.Net has fixed your issue or b) manually add your headers in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM