4

I'm trying to return a 401 (unauthorized) response code from ASP.NET Web API web method (POST) but I get a 404. Am I doing something wrong?

throw new HttpResponseException(HttpStatusCode.Unauthorized); (or setting it on the HttpResponseMessage)

Thanks

1
  • This question stackoverflow.com/questions/9852493/… shows the same method but it is wrapped in an if statement checking if the auth failed. Do you have any code in front of the exception that might be causing a valid 404 Error ? Commented May 9, 2012 at 14:41

1 Answer 1

2

Grrr - the problem was forms authentication, which hijacks the 401 and issues a redirect (302) to a non-existant login form, hence the 404. I just had to set the authentication mode to none in the web.config file.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, any idea how you do this if the WebApi is hosted in a FormsAuth MVC project?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.