开发者

Accessing ASP.NET MVC Action Parameters

This must be simple, but I can't seem to figure it out. I am setting an action parameter inside an action filter as follows:

public class MyFilter : ActionFilterAttribute
{
    public override void OnActionE开发者_运维百科xecuting (ActionExecutingContext filterContext)
    {
        filterContext.ActionParameters["MyParam"] = "MyValue";
    }
}

I am applying the filter to an 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?


Maybe you could use:

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

You can decorate whole controller with [MyFilter] or only one action.


I'm hoping this will work:

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

Since ValueProvider is what modelbinders use to get the values I would think it should be able to get the value set in your filter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜