0

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).

tereško
  • 58,060
  • 25
  • 98
  • 150
marcusstarnes
  • 6,393
  • 14
  • 65
  • 112

1 Answers1

1

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();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928