如何在apicontroller操作上返回403状态代码
本文关键字:状态 代码 返回 操作上 apicontroller | 更新日期: 2024-10-21 07:12:55
我希望返回类型保持为字符串,但希望响应代码为403。
这就是我目前拥有的。
[HttpGet]
public string test(string echoText)
{
// Not authorized
if (echoText == "403")
{
StatusCode(HttpStatusCode.Forbidden);
return "";
}
else
return echoText;
}
更新
我找到了下面的代码,但有没有其他方法不抛出异常?
throw new HttpResponseException(HttpStatusCode.Unauthorized);
您可以在操作中抛出HttpResponseException
:
[HttpGet]
public string test(string echoText)
{
// Not authorized
if (echoText == "403")
{
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
else
return echoText;
}