ActionFilterAttribute receives a call for all actions

Maybe I misunderstood the ActionFilterAttribute point, but now I have the following:

public class MyCustomAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { //do something useful here } } 

Then in my home controller, I have the following action methods:

 public class HomeController : Controller { public ActionResult Index() { return View(); } [MyCustom] public ActionResult Test() { return View(); } } 

Now I expect OnActionExecuting to OnActionExecuting when I try to access /Home/Test , but not when I try to access /Home/Index .

However, it starts for both action methods. I also checked inside OnActionExecuting that the Index action actually being called is actually happening.

Is it possible to have OnActionExecuting only the call that is called when the action method marked with the attribute is called?

+4
source share
1 answer

Check if MyCustomAttribute is MyCustomAttribute in the GlobalFilters collection in Global.asax .

+6
source

Source: https://habr.com/ru/post/1389040/


All Articles