asp.net-mvc – ASP.NET MVC ActionFilter参数绑定

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ASP.NET MVC ActionFilter参数绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果在action方法中有一个model-bound参数,那么你如何在action过滤器中得到该参数呢?
[MyActionFilter]
public ActionResult Edit(Car myCar)
{
    ...
}

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //I want to access myCar here
    }

}

有没有通过Form变量获取myCar?

解决方法

不确定OnActionExecuted,但可以在OnActionExecuting中执行:
public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // I want to access myCar here

        if(filterContext.ActionParameters.ContainsKey("myCar"))
        {
            var myCar = filterContext.ActionParameters["myCar"] as Car;

            if(myCar != null)
            {
                // You can access myCar here
            }
        }
    }
}
原文链接:https://www.f2er.com/aspnet/250371.html

猜你在找的asp.Net相关文章