How to fire OnActionExecuting in Web Api controller?

前端 未结 2 1168
臣服心动
臣服心动 2021-01-07 17:38

My api endpoints are using asp.net mvc (4) web api controllers.

Are there any events similiar to how mvc has OnActionExecuting?

Also, how to I access the Req

2条回答
  •  灰色年华
    2021-01-07 18:28

    Use action filters.

    public class MyActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
             //use filterContext.HttpContext.Request...
        }
    }
    

    For your controller action, apply the attribute

    [MyActionFilter]
    public Action MyAction(...)
    {
        //...
    }
    

    As Satpal mentioned in his comment, you might actually want to use AuthorizeAttribute to authorize access to your actions.

提交回复
热议问题