I want to do someth开发者_C百科ing 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?
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.
精彩评论