Using action parameters in custom Authorization Attribute in ASP.NET MVC3

前端 未结 4 1533
时光取名叫无心
时光取名叫无心 2020-12-13 19:42

I have a controller which should only request authorization when loaded with specific parameters. Like when the parameter ID is 8 for example.

I came up with using a

4条回答
  •  借酒劲吻你
    2020-12-13 20:24

    You need something like this.

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            int? id = GetId(filterContext);
    
            if (id.HasValue)
            {
              ...
            }
        }
    
        private static int? GetId(ActionExecutingContext filterContext)
        {
            int? Id = null;
    
            if (filterContext.ActionParameters.ContainsKey("Id"))
            {
                Id = (int?)filterContext.ActionParameters["Id"];
            }
        }
    

提交回复
热议问题