Reputation: 88
I´m trying to do this
throw new HttpResponseException(HttpStatusCode.Unauthorized); // 401
But this always returns 500(Internal server Error).
Example code:
using System.Net;
using Microsoft.AspNetCore.Mvc;
using System.Web.Http;
namespace Gral_WebAPI.Controllers
{
[Produces("application/json")]
public class LoginController : ApiController
{
[Microsoft.AspNetCore.Mvc.HttpGet]
[Microsoft.AspNetCore.Mvc.Route("[action]")]
public string GitTest()
{
throw new HttpResponseException(HttpStatusCode.Unauthorized); // 401
}
}
}
Anyone could help me?
Upvotes: 1
Views: 1979
Reputation: 12561
You could use return StatusCode(401);
instead. You should avoid using exceptions to control the flow of your application.
Do you have any middleware that's intercepting the exception and overriding the status code to 500?
Upvotes: 2