Get “Default” Route Url in Controller

穿精又带淫゛_ 提交于 2019-12-22 10:04:32

问题


Can anyone tell me what is the syntax for retreiving the actual URL for the "Default" Route?

I'd like to do something like:

string url = RouteTable.Routes["Default"].ToString();
//(even though that code is completely wrong)

so that I could have the url value of the route available to work with.

So far, I've been trying the .GetVirtualPath() method, but that only returns the route data for the current controller.

thanks

Dave


回答1:


A route can match any number of urls. So a route doesn't have a url. To get a url from a route you will have to supply it with the route data that you want the url for. To do that you simply use the RouteUrl() method on the Url property, Url.RouteUrl().

Update

If you want the url that will be generated if you supply the route with its default values you can do something like this:

var url = Url.RouteUrl("Default", ((Route)RouteTable.Routes["Default"]).Defaults);


来源:https://stackoverflow.com/questions/2356725/get-default-route-url-in-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!