Asp.net mvc 如何在asp.net mvc中从注册的路由中获取字符串url?

Asp.net mvc 如何在asp.net mvc中从注册的路由中获取字符串url?,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,在Global.asax中,我们使用以下方式注册路由: MapRoute(this System.Web.Routing.RouteCollection routes, string name, string url, object defaults, string[] namespaces) 有没有办法从代码中检索字符串url?具体而言,我感兴趣的是从注册路由的URL获取路由段,如下所示: "customRoutePrefix/{controller}

在Global.asax中,我们使用以下方式注册路由:

MapRoute(this System.Web.Routing.RouteCollection routes, 
    string name, 
    string url, 
    object defaults, 
    string[] namespaces)
有没有办法从代码中检索
字符串url
?具体而言,我感兴趣的是从注册路由的URL获取路由段,如下所示:

"customRoutePrefix/{controller}/{action}/{id}"
我需要查找“customRoutePrefix”或任何已注册路线的等效项

我想我可能可以使用
System.Web.Routing.RouteTable.Routes
来实现这一点,但我在那里没有找到它

这可能吗/我应该怎么做?

是的

您可以使用:

this.RouteData.Values["segment name"]
比如说,

this.RouteData.Values["controller"];
编辑1 这段代码将实现检索所有已注册路由URL的技巧

var res = new List<string>();

foreach (var item in RouteTable.Routes)
{
    var r = (Route)item;

    res.Add(r.Url);
}

this.ViewBag.Res = res;

对于案例
“customUnknownPrefix/{controller}/{action}/{id}”
如何访问第一段?在本例中,我要查找的值将是
“customUnknownPrefix”
,但它可以是任何已注册的内容。由于该部分不是路由值的一部分,相反,它是一个静态段可能您最好的方法是检查
this.Request.RawUrl
this.Request.Url.Segments
它是我使用
MapRoute()注册路由时定义的路由Url的一部分。我的问题是如何从已注册的路由中获取所有路由URL的列表
this.Request.RawUrl
this.Request.Url.Segments
将仅返回当前Url的值。。。不是所有注册的路线。我需要能够在没有请求的情况下获得这个URL列表。这样做了。谢谢你的帮助。
@foreach (var item in this.ViewBag.Res)
{
    <div>@item</div>
}
Chapter14/{controller}/{action}/{id}
Movies/{controller}/{action}/{id}
api/{controller}/{id}
{resource}.axd/{*pathInfo}
{resource}.svc/{*pathInfo}
{controller}/{action}/{id}
wcf/{*pathInfo}