Access ASP.NET MVC Action Parameters

It should be simple, but I can't figure it out. I set the action parameter inside the action filter as follows:

public class MyFilter : ActionFilterAttribute
{
    public override void OnActionExecuting (ActionExecutingContext filterContext)
    {
        filterContext.ActionParameters["MyParam"] = "MyValue";
    }
}

I apply a filter to the entire controller as follows:

 [MyFilter]
 public class HomeController : Controller
 {
      public ActionResult Index()
      {
           // How do I access MyParam here?
           return View();
      }
 }

}

How do I access MyParam inside an action method?

+3
source share
2 answers

Perhaps you could use:

[MyFilter]
public ActionResult Index(string MyParam)
{
       //Do something with MyParam           
       return View();
}

You can decorate the entire controller with [MyFilter]or with just one action.

+3
source

I hope this works:

var myParam = ValueProvider.GetValue("MyParam").RawValue as string;

Since ValueProviderthis is what modelers use to get values, I think it should be able to set the value in your filter.

0
source

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


All Articles