Posts

Showing posts with the label httpresponse

Ensure that a specific exception always results in a given HTTP response status code

Ensure that a specific exception always results in a given HTTP response status code Requirement I have an ASP.Net MVC application that works with a number of different libraries. As with most libraries, various function calls may cause one of many different exceptions to be thrown. Currently, whenever any exception is thrown, then MVC application 'handles' them and returns an "internal server error" (code 500) back to the client (eg. web browser). This is fine for most cases, however, there is one specific exception type (in this case UnauthorizedAccessException ) that I would like to result in an "Unauthorized" (code 401) status being sent in the response, instead of the usual 500 error. UnauthorizedAccessException Current Attempt I did a fair bit of research and it looks like the bets way to 'catch' all exceptions and process them is by using the Application_Error method. So I tried the following implementation of Application_Error in the MvcApp...