Ghooti Farangi
Ghooti Farangi

Reputation: 20156

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;

    [TestFilter(x=userID)]
    public ActionResult Index()
    {
    }
}

Upvotes: 2

Views: 1011

Answers (3)

Lee Smith
Lee Smith

Reputation: 6747

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

public  class TestFilterAttribute : ActionFilterAttribute
{


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



}

Upvotes: 1

WickyNilliams
WickyNilliams

Reputation: 5308

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 :-)

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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.

Upvotes: 1

Related Questions