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.