ASP.NET MVC ActionFilter参数绑定

Shl*_*omo 13 asp.net-mvc action-filter

如果在操作方法中有模型绑定参数,那么如何在操作过滤器中获取该参数?

[MyActionFilter]
public ActionResult Edit(Car myCar)
{
    ...
}

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

}
Run Code Online (Sandbox Code Playgroud)

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

eu-*_*-ne 11

不确定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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)