kenwarner
kenwarner

Reputation: 29130

Using ASP.NET MVC action parameters in custom attribute parameters

I want to do something like this

[MyAttribute(Message="Please upgrade to view " + name)]
public ActionResult Details(string name)
{
    ....
}

I know I can call filterContext.ActionDescriptor.GetParameters() from inside the attribute code itself, but is there any way to use them in the controller?

Upvotes: 0

Views: 1688

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

The correct way to achieve this is to use a custom action filter and inside use either filterContext.ActionDescriptor.GetParameters() or fetch the required parameter from RouteData. You cannot have dynamic values in an attribute declaration because attributes represent metadata that are baked into the assembly at compile time => .NET doesn't allow you this. Only static or constant parameters could be used at attribute declaration.

Upvotes: 2

Related Questions