Access HtmlHelpers from WebForm when using ASP.NET MVC

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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" }) %>

Method 2

Revised version of the code above for PageCommon … as it currently is it breaks.

public static class MvcPages{
public static UrlHelper GetUrlHelper(this System.Web.UI.Control c)
{
    var helper = new System.Web.Mvc.UrlHelper(c.Page.Request.RequestContext);
    return helper;
}

public static HtmlHelper GetHtmlHelper(this System.Web.UI.Control c)
{
    var httpContext = new HttpContextWrapper(HttpContext.Current);
    var controllerContext = new ControllerContext(httpContext, new RouteData(), new DummyController());
    var viewContext = new ViewContext(controllerContext, new WebFormView(controllerContext, "View"), new ViewDataDictionary(), new TempDataDictionary(), TextWriter.Null);

    var helper = new HtmlHelper(viewContext, new ViewDataBag());
    return helper;
} 

private class ViewDataBag : IViewDataContainer
{
    ViewDataDictionary vdd = new ViewDataDictionary();
    public ViewDataDictionary ViewData
    {
        get
        {
            return vdd;
        }
        set
        {
            vdd = value;
        }
    }
}

private class DummyController : Controller
{

}

}

Method 3

If you want to stay away from any MVC dependencies then this is the solution I came up with. It’s very close to the accepted answer. I have a class my webform pages inherit and this UrlHelper is available in the ASPX pages.

using System.Net.Http;
using System.Web;
using System.Web.Http.Routing;

public class ClassOtherPagesInherit {
    public UrlHelper Url = new UrlHelper(new HttpRequestMessage(new HttpMethod(HttpContext.Current.Request.HttpMethod), HttpContext.Current.Request.Url));
}

Then you can call this UrlHelper object like this

<%Url.Route("string", new {}) %>

Method 4

For those looking for an actual HtmlHelper or a cleaner way to use the urlHelper in a page:

public static class PageCommon
{
    public static System.Web.Mvc.UrlHelper GetUrlHelper(this System.Web.UI.Control c)
    {
        var helper = new System.Web.Mvc.UrlHelper(c.Page.Request.RequestContext);
        return helper;
    }
    class ViewDataBag : IViewDataContainer
    {
        ViewDataDictionary vdd = new ViewDataDictionary();
        public ViewDataDictionary ViewData
        {
            get
            {
                return vdd;
            }
            set
            {
                vdd = value;
            }
        }
    }
    public static System.Web.Mvc.HtmlHelper GetHtmlHelper(this System.Web.UI.Control c)
    {

        var v = new System.Web.Mvc.ViewContext();
        var helper = new System.Web.Mvc.HtmlHelper(v, new ViewDataBag());
        return helper;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x