Frage

When I try to throw the below forbidden ResponseException from my controller. An exception stating "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details." is catched in the catch block of the controller method. Need help in resolving this

throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
War es hilfreich?

Lösung

Just change your controller implementation to re-throw if it's an HttpResponseException:

try
{
    // action implementation
}
catch (Exception e)
{
    if (e is HttpResponseException)
    {
        throw e;
    }
    // error handling logic
}

But the better answer is that #1 - you should avoid be catching all exceptions, that's bad practice. And #2 - you should use an exception filter instead to do your error handling and not catch exceptions yourself.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top