我稍微修改了一下默认路由规则,如下所示:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id= (string)null } // Parameter defaults
);然后我可以将url设置为:
/Controller/Action/myParam
/Home/Index/MyParam默认操作索引为:
public ActionResult Index(string id)
{
//....
}我可以让这个参数生效。但我想在OnActionExecuting中获得参数。我该怎么做呢?
发布于 2009-07-23 17:11:31
您应该能够使用以下命令访问它:
public override void OnActionExecuting(ActionExecutingContext filterContext) {
string id = filterContext.RouteData.Values["id"];
//...
}发布于 2021-06-02 17:26:29
它可以从OnActionExecuting中的ActionArguments访问。
public override void OnActionExecuting(ActionExecutingContext context) {
string id = context.ActionArguments["id"].ToString();
//...
}发布于 2014-04-05 05:22:16
如果想要获取控制器、操作和所有参数,可以这样做
var valuesStr = new StringBuilder();
if (ctx.RouteData != null && ctx.RouteData.Values != null)
foreach (var v in ctx.RouteData.Values)
valuesStr.AppendFormat("/{0}", v.Value);
_logger.Info("executing {0}", valuesStr.ToString());
which results in the whole path 结果如下:
"/Get/Customer/215840"它也应该在多个参数上工作。
https://stackoverflow.com/questions/1173158
复制相似问题