Lior Lev
Lior Lev

Reputation: 143

In HttpStatusCode I get a status 500 again when I change it to 401

I have a function that catches errors And when an error occurs I check if it is an AJAX error And if this AJAX changes the status of the error to 401 Unauthorized I changed both the Result and the status Code like this:

     filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
     filterContext.Result = new HttpStatusCodeResult(System.Net.HttpStatusCode.Unauthorized);
     filterContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;

When I debug in c# I really see them 401 as what I changed and then it goes back to JS and there is 500 inside does anyone know why?

Thank you

Upvotes: 1

Views: 414

Answers (2)

Lior Lev
Lior Lev

Reputation: 143

Ok I was able to solve the problem

I should have simply added this sentence:

filterContext.ExceptionHandled = true;

Thanks

And good luck to everyone

Upvotes: 1

user10361591
user10361591

Reputation:

You should be throwing a HttpResponseException from your API method, not HttpException:

throw new HttpResponseException(HttpStatusCode.Unauthorized);

Upvotes: 0

Related Questions