Reputation: 6531
I have an ASP.NET MVC web application, and I've registered a number of routes in my Global.asax.
I would like to know how I can programmatically build (generate a string url) any one of those registered routes from within my Controller.
I did the same thing in Web Forms with .NET 4.0 using Page.GetRouteUrl(routeName, routeParams)
but can't figure out how to do the same thing in MVC (I'm an MVC newbie).
Upvotes: 0
Views: 272
Reputation: 1039508
You could use the UrlHelper class inside your controller action.
public ActionResult Index()
{
string address = Url.RouteUrl(new {
action = "foo", controller = "bar", id = "123"
});
// TODO: do something with the url
return View();
}
Upvotes: 1