How do I pass variables to a custom ActionFilter in ASP.NET MVC app

前端 未结 1 782
无人及你
无人及你 2020-12-13 19:30

I have a controller in my MVC app for which I\'m trying to log details using a custom ActionFilterAttribute, by using the onResultExecuted method.

I read thi

相关标签:
1条回答
  • 2020-12-13 20:04

    Approach - 1

    Action Filter

    public class MyActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
    }
    

    Action Method

    [MyActionFilter]
    public ActionResult Index()
    {
        ViewBag.ControllerVariable = "12";
        return View();
    }
    

    enter image description here

    If you pay attention to the screenshot, you can see the ViewBag information

    Approach - 2

    Action Filter

    public class MyActionFilter : ActionFilterAttribute
    {
        //Your Properties in Action Filter
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }
    }
    

    Action Method

    [MyActionFilter(Property1 = "Value1", Property2 = "Value2")]
    public ActionResult Index()
    {
        return View();
    }
    
    0 讨论(0)
提交回复
热议问题