Access HtmlHelpers from WebForm when using ASP.NET MVC

前端 未结 3 1540
鱼传尺愫
鱼传尺愫 2020-12-10 16:24

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\", \"Control         


        
3条回答
  •  不思量自难忘°
    2020-12-10 16:53

    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;
        }
    }
    

提交回复
热议问题