Reputation: 877
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.
Upvotes: 8
Views: 1801
Reputation: 469
I have worked a lot with custom login and handling errors. Have learned a lot. And now I am at Linux and can't give exact response but I suggest look around the HttpResponse accessor and HttpResponse.Write() method. It will replace whole content of error page without any pretending.
Additional you can setup this page in web.config file.
Added: Put in your web.config this:
<configuration>
<system.webServer>
<httpErrors>
<remove statusCode="500" />
</httpErrors>
</system.webServer>
</configuration>
Upvotes: 0
Reputation: 877
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
Upvotes: 5
Reputation: 25108
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.
Upvotes: 1