Temir
Temir

Reputation: 1

How to get RouteData.Values["action"].ToString() from User Attribute

    [CheckAccessMethod(RouteData.Values["action"].ToString())]
    [HttpGet]
    public IActionResult Get(){...}

    class CheckAccessMethodAttribute : Attribute
    {
         string MethodName { get; set; }

        public  CheckAccessMethodAttribute(string methodName)
        {
            MethodName = methodName;
        }

    }

I can’t get the current request route. I want to create method access logic for users

Upvotes: 0

Views: 607

Answers (1)

Shoejep
Shoejep

Reputation: 4839

One option would be to use the ActionFilterAttribute, then you'd have access to the ResultExecutingContext which has the route data you need. More information here

public class MyActionFilterAttribute : ActionFilterAttribute
{
     public override void OnResultExecuting(ResultExecutingContext context)
     {
         var action = context.RouteData.Values["action"];
         //do something with action here

         base.OnResultExecuting(context);
     }
 }

Upvotes: 1

Related Questions