开发者

Get "Default" Route Url in Controller

开发者 https://www.devze.com 2022-12-21 09:08 出处:网络
Can anyone tell me what is the syntax for retreiving the actual URL for the \"Default\" Route? I\'d like to do something like:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消