2009-08-13 15 views
6

Estoy agregando un WebForm del cual me gustaría resolver las rutas a las URL. Por ejemplo, en MVC yo sólo usaríaAcceda a HtmlHelpers desde WebForm al utilizar ASP.NET MVC

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

lo tanto, si usted tiene una manera de llegar a la misma URL desde un formulario Web en la misma aplicación, se agradecería.

+0

se necesita una instancia de UrlHelper, no HtmlHelper –

+3

probar esto: http://stackoverflow.com/questions/724153/how-do-i-construct-a-route-without-viewcontext-in-asp -net-mvc –

Respuesta

15

probar algo como esto en su formulario web:

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

Para aquellos que buscan una HtmlHelper real o una forma más limpia de utilizar el UrlHelper en una página:

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

Esto no fue exitoso para mí. Creé clase Class PageCommon. Y luego agregué el fragmento en MasterPage.master. '<% = this.GetHtmlHelper(). Parcial (" Error ")%>' y recibió una ArgumentNullException. –

3

versión revisada del Código arriba para PageCommon ... ya que actualmente se rompe.

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 
{ 

} 

} 
+0

Esto no tuvo éxito para mí. Creé clases MvcPages. Y luego agregué el fragmento en MasterPage.master. '<% = this.GetHtmlHelper(). Parcial (" Error ")%>' y recibió una InvalidOperationException. –

Cuestiones relacionadas