开发者

Pass a non static value to a actionfilter

Is ther any way to pass a non static value to a actionfilter parameter like below?

public class ProcuctController : Controller
{
    private int userID = 1;

    [Test开发者_如何转开发Filter(x=userID)]
    public ActionResult Index()
    {
    }
}


No, there isn't. Attributes are baked into the metadata of the resulting assembly and all values should be known at compile time. You can pass only constant values as attribute properties. And that's .NET limitation, not MVC.


depending on the situation, you could always pass in some kind of key to the filter, and then inside the filter you could do a lookup for the corresponding value in whichever datasource you prefer. this would allow you to get non-static data in your method, though it would be nice if it were as simple as your example :-)


You can't pass it in but you could do something like:

public  class TestFilterAttribute : ActionFilterAttribute
{


    public string UserId
    {
        get
        {
            return AppSettings["UserId"];
        }
    }



}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜