如何从代码隐藏中访问RouteData?

Dea*_*ane 48 asp.net routing url-routing

使用ASP.Net路由时,如何从代码隐藏中获取RouteData?

我知道你可以从RouteHandler的GetHttpHander方法获得它(你得到了RequestContext),但是你可以从代码隐藏中得到它吗?

有没有...

RequestContext.Current.RouteData.Values["whatever"];
Run Code Online (Sandbox Code Playgroud)

...你可以全局访问,就像你可以使用HttpContext一样?

或者是否只是从RouteHandler内部访问RouteData?

Hos*_*Aly 158

您还可以使用以下内容:

//using System.Web;
HttpContext.Current.Request.RequestContext.RouteData
Run Code Online (Sandbox Code Playgroud)

  • 这是一种更高效的方法. (15认同)

Rup*_*tes 34

您可以使用以下内容:

RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这样做会再次调用路由,这就像N路由表中的N个请求一样. (10认同)