Best way to abort/cancel action and response from ActionFilter

后端 未结 6 1639
清歌不尽
清歌不尽 2020-12-24 13:51

Best way to abort/cancel action from ActionFilter

I\'ve got this ActionFilter, and it\'s suppose to end the connection immediately and return a 401 Unau

6条回答
  •  天涯浪人
    2020-12-24 14:13

    The answer that @OdeyinkaOlubunmi is correct for Web API or specifically System.Web.Http.Filters.ActionFilterAttribute but it can't be used for System.Web.Mvc.ActionFilterAttribute. AuthorizeAttribute and overriding AuthorizeCore is a good way to go but if you use @Vadim's example for a GlobalFilter you will end up with the following error in a standard configuration:

    HTTP Error 404.15 - Not Found The request filtering module is configured to deny a request where the query string is too long.

    This is because the default /Login?ReturnUrl= will keep appending new values until the query string causes an exception.

    The way I have solved it for MVC is like this:

    public class DebugActionFilter : System.Web.Mvc.ActionFilterAttribute
    {
      public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext actionContext)
      {
        actionContext.Result = new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
        return;
      }
    }
    

提交回复
热议问题