If it matters, this开发者_开发问答 is in ASP.NET MVC2 - how do I do this - how do I apply the "id" parameter to the attribute? This following syntax obviously does not work :)
[AuthorizeProject(ProjectId = id)]
public ActionResult Browse(int id)
{
// Stuff
return View();
}
Instead of trying to set in the Attribute constructor, what about setting ProjectId in one of the event handlers? You would have access to the RouteData via the context object.
public override void OnAuthorization(AuthorizationContext filterContext)
{
ProjectId = filterContext.RouteData.Values["id"].ToString();
}
You just need to make sure that you use it on methods that have an id parameter or that you do some type of validation check in the OnAuthorization method.
The id parameter of the Browse function is not in scope at the moment the attribute is evaluated. You can't do this therefore.
Attribute parameters and property values have to be known at compile time, usually literals.
You cannot pass method parameters to an attribute.
Depending on what you are trying to do there will be a different approach, but without the details of what you are trying to do suggestions are hard.
精彩评论