如果url是 /home/index?id=3 直接Request就ok。
但是如果路由设定为:{controller}/{action}/{id}
url是 /home/index/3 这时想在页面View中获取参数id的值,该怎么获取?
查了下资料好多人都利用Action获取到参数值后,用Viewdata传到View中 例如 Controlers中的phonelist这样定义 public ActionResult phonelist(int id) { ViewData["id"] = id; return View(); } 其实,没有必要,只要在view中这样获取就可以:
<%=Html.ViewContext.RouteData.Values["id"]%>
就算没有id的参数也不会报错。
同样:<%=Request.RequestContext.RouteData.Values["id"] %>和 <%=Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext).Values["id"]%>
也可以取到。
注:在用户控件中是无法直接访问到RouteData,RouteData是Page对象中的属性,
所以需要在用户控件中使用this.Page.RouteData来获取参数
使用this.Page.RouteData.Values["id"]来获取参数的值
转载于:https://www.cnblogs.com/zoro-zero/p/4464963.html
相关资源:JAVA上百实例源码以及开源项目