开发者

Using ASP.NET MVC action parameters in custom attribute parameters

开发者 https://www.devze.com 2023-03-08 19:47 出处:网络
I want to do someth开发者_C百科ing like this [MyAttribute(Message=\"Please upgrade to view \" + name)]

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

精彩评论

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