views:

160

answers:

1

Hi Guys

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: 

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);
Mattias Jakobsson
The default route has defaults for the controller and action, which is what I presume he wants (but I still wonder why!).
Craig Stuntz
@Mattias, thanks but this code returns the route that I'm already on. What I'm looking for is to get the "Default" url (i.e. the equivalent URL that RedirectToRoute("Default") would redirect to. @Craig, it's a long story (probably 'cause I'm doing it wrong!) but I want to be able to return the Default route to let jQuery do the redirecting.
DaveDev
It doesn't return the route or url that you are already on, it uses the route values of the current route and supply them to the route you specify, "Default" in this case. You just happen to already be using that route for your current url. Check my updated answer on how you can get the default values from a route.
Mattias Jakobsson
@Mattias, thanks man. I get the point you're making too - Default refers to the pattern, as opposed to a specific URL.
DaveDev