Get "Default" Route Url in Controller
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"].ToStrin开发者_运维百科g();
//(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
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);
精彩评论