The correct way to throw exceptions in ASP.NET MVC 4 ActionFilterAttribute

Note that this is for ApiController in MVC 4, although I think it should not change anything.

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. } } } 
+8
c # asp.net-mvc-3
source share
1 answer

You can set the result property of the HttpActionContext :

 public override void OnActionExecuting(HttpActionContext actionContext) { if (checkVerified()) { actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } } 

you might just throw away:

 throw new HttpResponseException(HttpStatusCode.Unauthorized); 

but I did not check it.

+11
source share

All Articles