How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

后端 未结 3 1841
一向
一向 2020-12-01 01:13

I\'m trying to create a custom ActionFilter which operates on a set of parameters that would be passed to it from the controller.

So far, my customer ActionFilter lo

3条回答
  •  被撕碎了的回忆
    2020-12-01 01:15

    Attributes are essentially metadata added to a type. They can only use const values, instead of instance variables. In your case you are tying to pass in your instance variables of genisisRepository, etc. This will fail to compile as they are not compile time constants.

    You should look into Dependency Injection for Action Filters to achieve this, typically using an IoC container.

    Also, if your ActionFilter is performing a post ActionResult action, such as OnActionExecuted, you could probably get away with storing something in the route data:

    public ActionResult Index()
    {
      ControllerContext.RouteData.DataTokens.Add("name", "value");
      return View();
    }
    

提交回复
热议问题