views:

349

answers:

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.

+4  A: 

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

eu-ge-ne