1

How can I generate the URLs corresponding to the controller, action, and parameters (for a given ASP.NET MVC project) in another project (a class library used for testing)?

All I have found so far is HtmlHelper.GenerateRouteLink, but didn't find yet how to pass the correct request context and route collection.

alexandrul
  • 12,856
  • 13
  • 72
  • 99

1 Answers1

4

Try this:

var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var context = new Mock<HttpContextBase>();

var urlHelper = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

var url = urlHelper.Action("action", "controller", new { id = ... });

From SO - ASP.NET MVC: Unit testing controllers that use UrlHelper

Community
  • 1
  • 1
eu-ge-ne
  • 28,023
  • 6
  • 71
  • 62