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