Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parameter in OnActionExecuting?

Tags:

asp.net-mvc

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?

like image 808
KentZhou Avatar asked Jul 23 '09 17:07

KentZhou


1 Answers

It can be accessible from ActionArguments inside OnActionExecuting.

public override void OnActionExecuting(ActionExecutingContext context) {
    string id = context.ActionArguments["id"].ToString();
    //...
}
like image 78
Nayeem Bin Ahsan Avatar answered Oct 20 '22 03:10

Nayeem Bin Ahsan