cs0815
cs0815

Reputation: 17388

passing action method parameter to ActionFilterAttribute in asp.net mvc

I know that I can use the filterContext to get to it. However, this is not very flexible if the action method parameter is named differently. This should work:

[HttpGet]
[NewAuthoriseAttribute(SomeId = id)]
public ActionResult Index(int id)
{
    ...

public class NewActionFilterAttribute : ActionFilterAttribute
{   
    public int SomeId { get; set; }
    ...

but it does not (it does not even compile). Any ideas?

Upvotes: 38

Views: 54586

Answers (3)

In the new versions, you only need to define a property inside the class and set the value above the methods.

public enum Roles
{
    Admin = 1,
    User
}

public class CustomAuthorizationAttribute : ActionFilterAttribute

{
    public Roles Role { get; set; }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (Role == Roles.Admin)
        {
            throw new UnauthorizedException(HttpStatusCode.Unauthorized, "AccessDenied", ResponseMessage.InvalidToken);
        }

        base.OnActionExecuting(context);
    }
}

  [CustomAuthorization(Role = Roles.Admin)]
    public IActionResult OnlyAdmin()
    {
        return Content("Hi Admin");
    }

Upvotes: 0

user2007801
user2007801

Reputation:

Edit

I am assuming that you are looking to make the Alias of Parameter name. This is giving you the flexibility to have multiple Alias of your paramater Name.

enter image description here

ActionParameterAlias.ParameterAlias Overloads

enter image description here

If so, you can give alias like below.

[ParameterAlias("Original_Parameter_Name", 
                 "New_Parameter_Name")]
[ParameterAlias("Original_Parameter_Name", 
                 "New_Parameter_Name1")]
[ParameterAlias("Original_Parameter_Name", 
                 "New_Parameter_Name2")]
[ParameterAlias("Original_Parameter_Name", 
                 "New_Parameter_Name3")]

public ActionResult ActionMethod(Model ParameterValue) { return View(ParameterValue); }


Original Post

Try this one.

Attribute

public class NewAuthoriseAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey("id"))
        {
            var id = filterContext.ActionParameters["id"] as Int32?;
        }
    }
}

Action Method

Make sure to set the Parameter type nullable to avoid RunTime Crash.

[NewAuthoriseAttribute]
public ActionResult Index(Int32? id)
{
}

Upvotes: 2

Jasen
Jasen

Reputation: 14250

Building on the answer from @Pankaj and comments from @csetzkorn:

You pass the name of the parameter as a string then check the filterContext

public class NewAuthoriseAttribute : ActionFilterAttribute
{
    public string IdParamName { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(IdParamName))
        {
            var id = filterContext.ActionParameters[IdParamName] as Int32?;
        }
    }
}

[NewAuthorizeAttribute(IdParamName = "fooId")]
public ActionResult Index(int fooId)
{ ... }

Upvotes: 86

Related Questions