views:

22

answers:

3

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();
    }
A: 

The id parameter of the Browse function is not in scope at the moment the attribute is evaluated. You can't do this therefore.

Peladao
A: 

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.

Richard
+1  A: 

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.

Jonathan Bates
Excellent - thanks. I did something similar.
George R