C# – Need to override Http Response Code from asmx web service

apache-flexcexceptionhttpsoap

My web sevices support flex/flash clients and, upon unhandeld exceptions, throw custom faults that extend System.ServiceModel.FaultException.

I have been informed that flex/flash can't read these custom faults if the the http response code is different from 200. This is documented as flex/flash bug: http://bugs.adobe.com/jira/browse/SDK-11841

I need to override the http return code upon unhandled exceptions. I have attempted to do this by including this code in global.asax (this hack has been documented as a work-around):

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    if (Response.StatusCode != 200)
    { // fix response code for flex
        Response.StatusCode = 200;
    }
}

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{    
    if (Response.StatusCode != 200)    
    { // fix response code for flex        
         Response.StatusCode = 200;
     }
}

But alas, my http return code comes back as 500 when an unhandled exception is encountered

Any ideas?

Best Answer

You will probably need to add the following code before changing the response status code:

HttpContext.Current.ClearError()

That should keep your status code changes from getting overridden.