Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Response status code causes unwanted error text at top of custom error page

UPDATE:

To ensure that the correct status code is returned to the browser, the error.aspx has this line:

<% Response.StatusCode = 500; %>

Removing it removes the unwanted text, but I want the correct status code, so I suppose that is the new question...how!?

Setting the response status code has the same result:

HttpContext.Current.Response.StatusCode = 500;

Will update question title.

PRE-UPDATE:

Due to some legacy code/configuration, we have a slightly unusual custom error pages setup, which handles exceptions in the global.asax and uses Server.Transfer() to present the appropriate .aspx error page, like so:

public void Application_Error(object sender, EventArgs e)
{
    // abbreviated to clear out logging and some other logic that determines which error page to show

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Server.ClearError();

    var context = HttpContext.Current;
    if (context != null)
    {
        var errorPage = "~/ErrorPages/error.aspx";
        context.Server.Transfer(errorPage);
    }
}

The issue is that when error.aspx is shown to a remote user an error message (and a new opening body tag...) is prepended to the page, so the first line of the page is unstyled text saying:

The page cannot be displayed because an internal server error has occurred.

Not ideal, and, though perhaps I am Googling the wrong things, it does not seem to be a problem that is well documented or frequently discussed.

Any ideas welcome. The error.aspx code is pretty generic but I am happy to post if it might help - please just comment.

like image 722
GP24 Avatar asked Nov 03 '15 10:11

GP24


People also ask

Which is used for configuring the custom error page to custom error code in web config file?

The <customErrors> element under system. web in web. config is used to configure error code to a custom page. It can be used to configure custom pages for any error code 4xx or 5xx.

Which attribute of the custom errors is used to set the error page URL?

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 .

How to display custom error page in ASP net?

Steps for Custom Error PageSet <customErrors> setting in Web. Config file of the application. Pass defaultRedirect and mode attributes in <customErrors>. If you want to set your application level exception should redirect to your custom error page, you can do this by going to global.

What is custom error in asp net?

OFF: ASP.NET uses its default error page for both local and remote users in case of an error. ON: Custom error will be enabled for both local as well as remote client. REMOTE ONLY: The custom error page will be shown to a remote user only, the local user will get the built-in error page.


2 Answers

The fix was to add this to the web.config:

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

Based on this answer:

IIS7 Overrides customErrors when setting Response.StatusCode?

And this blog post:

http://blogs.iis.net/ksingla/what-to-expect-from-iis7-custom-error-module

like image 199
GP24 Avatar answered Oct 17 '22 13:10

GP24


Did you tried to End the request?

HttpContext.Current.Response.End()

MSDN Response.End(): Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event.

like image 36
Tomas Kubes Avatar answered Oct 17 '22 13:10

Tomas Kubes