1

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();
    }
4
  • Did you forget the other half of your question? Currently, it makes no sense at all! Commented Aug 17, 2010 at 7:56
  • Not really - I want to grab that "id" being fed to Browse() and feed it to AuthorizeProject (custom attribute), but I can't do that apparently. Commented Aug 17, 2010 at 8:01
  • You would have to do that before the method is called. Normally with the url router. Commented Aug 17, 2010 at 8:07
  • are you inheriting from AuthorizeAttribute with your AuthorizeProjectAttribute? Commented Aug 17, 2010 at 8:13

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.