0

I want to have control over my Http Exceptions to the point of being able to dictate what is in the body of the response. If I throw a standard HttpException(404, "not found"); then using fiddler I can see the 404 status code is set but there is all this garbage html thats shoved into the response body.

Is there any way to override or extend the HttpException class to allow you to inject your own content into the response body?

I have attempted to implement my own CustomHttpException that like the HttpException extends the ExternalException class but no matter how I cut it the system always throws a 500 error with the same standard html in the body.

Any help?

David
  • 1,731
  • 24
  • 37

1 Answers1

2

No, no, no.

Take a look at CustomErrors.

Basically you do something like this

    <customErrors defaultRedirect="GenericError.htm"
                  mode="RemoteOnly">
       <error statusCode="404"
              redirect="NotFound.aspx"/>
    </customErrors>
archil
  • 39,013
  • 7
  • 65
  • 82
  • Thats going on the assumption that I'm returning a user friendly html page to a browser. How do I deal with ajax calls? – David Mar 20 '12 at 12:48
  • Not really, you can redirect errors do dynamic .aspx urls, where you can manipulate resulting content in code-behind. Are you using asp.net webforms or asp.net-mvc? – archil Mar 20 '12 at 12:51
  • MVC 3, I want a discreet Rest service that can return proper http status codes and either json or xml to be consumed by the browser/ajax/mobile app etc. – David Mar 20 '12 at 12:57
  • You can derive from [HandleErrorAttribute](http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx) and control resulting content then. – archil Mar 20 '12 at 12:59