How to plug method parameters into custom attribute

前端 未结 4 1088
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 08:13

I have a custom Attribute called AuthoriseAttribute whose constructor looks like this:

public AuthoriseAttribute(int userId)
{
  .. blah
}

This

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 08:58

    There is a way to do this _in ASP.NET MVC_ with action-methods (not with attributes in general)

    public class CustomAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            int userId = (int)filterContext.ActionParameters["userId"];
        }
    }
    

提交回复
热议问题