Given the following code:
public class MyActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var a = filterContext.ActionParameters["someKey"];
var b = filterContext.RouteData.Values["someKey"];
base.OnActionExecuting(filterContext);
}
}
What is the difference between a
and b
?
When should we be using action parameters over route data? What is the difference?
When you use ActionParameters
on OnActionExecuting
, you can change the values that are sending by the client-side before processing an action, for sample:
public class MyActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.ActionParameters["customerId"] = 852;
base.OnActionExecuting(filterContext);
}
}
If you have an action using a customerId parameter, you will get the value setted on the action filter, since your action has the filter, for sample:
When you request any url like this: /customer/detail/123
, you will get 852
value on CustomerId:
[MyAction]
public ActionResult Detail(int customerId)
{
// customerId is 852
return View();
}
RouteData
is just about the values are on the url, processing by route tables.