tags:

views:

179

answers:

2

I modify the default route rule a little bit as below:

routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id= (string)null }  // Parameter defaults
);

Then I can set url as:

/Controller/Action/myParam
/Home/Index/MyParam

The default Action Index would be:

public ActionResult Index(string id)
{
  //....
}

I can get the param in action. But I want to get the param in OnActionExecuting. How can I do it?

+3  A: 

You should be able to access it with :

public override void OnActionExecuting(ActionExecutingContext filterContext) {
    string id = filterContext.RouteData.Values["id"];
    //...
}
çağdaş
This is also exposed via the ActionExecutingContext.ActionParameters property. The ActionParameters property allows you to see or change the parameters that will actually be passed to the action method.
Levi
A: 

From your filterContext you should be able to get whatever you need.


public class MyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
       //Do your stuff here
    }
}

[MyAttribute]
public ActionResult Index(string id)
{
  //....
}
Freddy