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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.