ログインメソッドなどを使用してAPIを作成しようとしています。ログインに問題がある場合は、カスタムHTTPエラーコードをカスタム応答本文とともにクライアントに返したいと思います。問題は、エラーが発生すると、リクエストがホームページにリダイレクトされることです。
[HttpPost]
[AllowAnonymous]
public JsonResult ApiLogIn(LoginModel model)
{
if (ModelState.IsValid)
{
var outcome = _authService.LogIn(model);
if (outcome == LogInOutcome.Success)
{
return Json(new { }); // Empty on success
}
else
{
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Json(new { reason = outcome.ToString() });
}
}
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { }); // Empty or invalid model
}
エラー時にリダイレクトされないようにするにはどうすればよいですか?