开发者

Using ASP.NET MVC action parameters in custom attribute parameters

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜