[英]Correct way to raise an exception in an ASP.NET MVC 4 ActionFilterAttribute
Note that this is for an ApiController in MVC 4 although I think it shouldn't change anything. 请注意,这是针对MVC 4中的ApiController,尽管我认为它不应该改变任何东西。
public class OAuthFilter : System.Web.Http.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (checkVerified())
{
// How to raise a 401 or some other type of exception.
}
}
}
You can set the result property of the HttpActionContext
: 您可以设置
HttpActionContext
的result属性:
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (checkVerified())
{
actionContext.Response =
new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
}
you may probably just throw: 你可能只是扔:
throw new HttpResponseException(HttpStatusCode.Unauthorized);
but I haven't checked that one. 但我没有检查那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.