Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionExecutingContext - ActionParameters vs RouteData

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?

like image 217
RPM1984 Avatar asked Nov 01 '12 01:11

RPM1984


1 Answers

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.

like image 109
Felipe Oriani Avatar answered Sep 20 '22 15:09

Felipe Oriani