How to send a Status Code 500 in ASP.Net and still write to the response?
Asked Answered
M

3

70

I have an ASP.Net single-file web service (a .ashx file containing an IHttpHandler implementation) which needs to be able to return errors as responses with 500 Internal Server Error status codes. This is a relatively straightforward thing to do in PHP:

header("HTTP/1.1 500 Internal Server Error");
header("Content-Type: text/plain");
echo "Unable to connect to database on $dbHost";

The ASP.Net (C#) equivalent should be:

Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Context.Response.ContentType = "text/plain";
Context.Response.Write("Unable to connect to database on " + dbHost);

Of course, this doesn't work as expected; instead, IIS intercepts the 500 status code, trashes whatever I've written to the Response object, and sends either debug info or a custom error page, depending on how the app is configured.

My question - how can I suppress this IIS behaviour and send error information directly from my IHttpHandler implementation?

This app is a port from PHP; the client-side is already written, so I'm essentially stuck with this spec. Sending errors with a 200 status code sadly doesn't fit the mould.

Ideally, I need to control the behaviour programmatically, because this is part of an SDK we want to distribute without any "edit this file" and "change this IIS setting" supplementary instructions.

Thanks!

Edit: Sorted. Context.Response.TrySkipIisCustomErrors = true was the ticket. Wow.

Microorganism answered 21/12, 2010 at 3:11 Comment(2)
If possible I'd avoid giving out internal info such as the database location on a production site - hackers love getting this stuff, makes their attacks much easier.Redfin
Good point, but this is a secured web service, not a website. Database connection isn't even attempted without previously successful HTTP authentication :)Microorganism
M
119

Context.Response.TrySkipIisCustomErrors = true

Microorganism answered 22/12, 2010 at 1:10 Comment(2)
Supported in IIS7 - msdn.microsoft.com/en-us/library/…Brownedoff
It is answers like this that make me realize that combing thru framework object reference is becoming a lost art. If I could upvote twice I would.Literatim
M
12

I have used the following in the past and been able to throw a 503 error with a custom message using the code shown below in the Page_Load method. I use this page behind a load balancer as the ping page for the load balancer to know if a server is in service or not.

Hope this helps.

        protected void Page_Load(object sender, System.EventArgs e)
    {
        if (Common.CheckDatabaseConnection())
        {
            this.LiteralMachineName.Text = Environment.MachineName; 
        }
        else
        {
            Response.ClearHeaders();
            Response.ClearContent(); 
            Response.Status = "503 ServiceUnavailable";
            Response.StatusCode = 503;
            Response.StatusDescription= "An error has occurred";
            Response.Flush();
            throw new HttpException(503,string.Format("An internal error occurred in the Application on {0}",Environment.MachineName));  
        }
    }
Mymya answered 21/12, 2010 at 3:28 Comment(3)
Hi Paige. Was this from a .ashx file or a regular .aspx file? I tried adding the clear/flush methods and it made no difference. I also tried throwing a new HttpException and I just got some extra unwanted HTML nonsense. Nice try though.Microorganism
Neil. This was from a regular .aspx page. Sorry I missed that you were working with a .ashx file.Mymya
It works in .ashx handler also. Response.Flush() is what i was missing.ThanksSleeping
D
0

You may wish to set a customErrors page (configurable via the web.config). You can store your content across requests in Session (or via an alternative mechanism) then have asp.net configured to display the custom error page which in-turn displays your custom output.

A word of caution, though: If the 500 is being caused because of a fundamental problem with the application (i.e. StackOverflowException) and you try to display a page that depends on asp.net (i.e. MyCustomErrors.aspx), you may end up in a loop.

For more information, check out this page.

Displant answered 21/12, 2010 at 3:17 Comment(1)
Thanks Bobby. I'm familiar with the customErrors directive, but unfortunately it relies on editing the web.config file, which our users may not be able to do. One possible solution is to modify the web.config file programmatically, but this is obviously less than ideal.Microorganism

© 2022 - 2024 — McMap. All rights reserved.