[SOLVED] Access HtmlHelpers from WebForm when using ASP.NET MVC

Issue

I am adding a WebForm from which I would like to resolve routes to URLs. For example, in MVC I would just use

return RedirectToAction("Action", "Controller");

So, if you have a way of getting to that same URL from a WebForm in the same application, it would be appreciated.

Solution

Try something like this in your Webform:

<% var requestContext = new System.Web.Routing.RequestContext(
       new HttpContextWrapper(HttpContext.Current),
       new System.Web.Routing.RouteData());
   var urlHelper = new System.Web.Mvc.UrlHelper(requestContext); %>

<%= urlHelper.RouteUrl(new { controller = "Controller", action = "Action" }) %>

Answered By – eu-ge-ne

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *