Связывание параметров ASP.NET MVC ActionFilter

Если у вас есть параметр привязки модели в методе действий, как вы можете получить этот параметр в фильтре действий?

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

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

}

В любом случае, чтобы получить myCar, не пройдя через переменные формы?

Ответы

Ответ 1

Не уверен в 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
            }
        }
    }
}