mardi 17 novembre 2015

How to pass variables to custom ActionFilterAttribute

What are my options for passing variables into my MyCustomAttribute class?

Currently the class is defined as:

public class MyCustomAttribute : ActionFilterAttribute
{
    public string MyIDParam { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var myID = filterContext.ActionParameters[MyIDParam] as Int32?;
        ..... logic ....
    }
}

Now in my controller I have this:

[MyCustomAttribute(MyIDParam = "id")]
public ActionResult Report(int id)
{
    Guid userId = GetUserInfo();

    ... logic ...
}

In this case I would like to be able to pass "id" and "userId" to MyCustomAttribute class.

Is this possible? Doesn't the MyCustomAttribute get executed before the contents of the Report method?

Aucun commentaire:

Enregistrer un commentaire