8

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.

GP24
  • 867
  • 2
  • 13
  • 28
  • Hi @Siva Gopal, I did try that, and various other things. Turned out I needed to set the existingResponse in web.config. See my answer. Thanks for the ideas anyway. – GP24 Nov 09 '15 at 13:35

3 Answers3

5

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

Community
  • 1
  • 1
GP24
  • 867
  • 2
  • 13
  • 28
1

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.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • Ending the response then trying to server transfer causes an HttpException. An interesting idea though. What was your thinking here? Maybe there is something in this... – GP24 Nov 09 '15 at 09:19
  • Turns out it was this line in the aspx: <% Response.StatusCode = 500; %>. Only happens when accessed remotely. I want the correct status code to be returned to the browser though. – GP24 Nov 09 '15 at 10:50
0

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>
vitalii
  • 443
  • 6
  • 10
  • Thanks @vitalii, that's not a bad idea! The response is already cleared by the time I attempt the server transfer though. And I know you can do this more elegantly with the web.config, but I am stuck with classic pipeline mode on this project so not an option. – GP24 Nov 06 '15 at 15:14
  • @GP24 Try to add __HttpResponse.Clear()__ method before __Transfer__. It clears a little different buffer than _HttpContext.Current.Response.Clear()_ . – vitalii Nov 09 '15 at 08:50
  • Thanks @vitalii, no change unfortunately but worth checking. Also, are you sure they are different? – GP24 Nov 09 '15 at 09:21
  • I am not sure at 100%. At microsoft site description about clear a typed different buffered, I can't debug it now to be sure but in global.asax its better to use __HttpResponse__ as it accessible here directly. Your problem seems for me strange now as for me __HttpResponse.Write()__ worked perfect (nonetheless I use web.config afterwards) and so __Clear()__ should work too. And I still guessing that the text prepending by server server handling error status and after that you handle response. – vitalii Nov 09 '15 at 09:31
  • Turns out it was this line in the aspx: <% Response.StatusCode = 500; %>. Only happens when accessed remotely. I want the correct status code to be returned to the browser though. – GP24 Nov 09 '15 at 10:50
  • It's possible. Just put in config custom errors that will be empty string/page. Or find more perfect solution for disabling output of server errors. I added example into answer. – vitalii Nov 09 '15 at 13:17
  • @GP24 Added web.config configuration. I guess it will resolve your problem. – vitalii Nov 09 '15 at 13:32
  • It was a web.config issue, but not this one (which I don't think will work in classic pipeline mode, but I could be wrong). See my answer. Thanks for the ideas though @vitalii. Much appreciated. – GP24 Nov 09 '15 at 13:33