-
-
Save Boggin/3951330 to your computer and use it in GitHub Desktop.
Utility class to render MVC Partials & Actions in WebForms (or server side controls).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% WebFormMvcUtil.Html.RenderAction("Index", "PhoneNumber"); %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Website | |
{ | |
using System.IO; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
public class WebFormMvcUtil | |
{ | |
private static HtmlHelper html; | |
public static HtmlHelper Html | |
{ | |
get | |
{ | |
return html ?? (html = new HtmlHelper(CreateViewContext(), new ViewUserControl())); | |
} | |
} | |
public static ViewContext CreateViewContext() | |
{ | |
// get a wrapper for the legacy WebForm context | |
var httpContextWrapper = new HttpContextWrapper(HttpContext.Current); | |
// create a mock route that points to the empty controller | |
var routeData = new RouteData(); | |
routeData.Values.Add("controller", "WebFormController"); | |
// create a controller context for the route and http context | |
var controllerContext = | |
new ControllerContext( | |
new RequestContext(httpContextWrapper, routeData), | |
new WebFormController()); | |
// find the partial view using the viewengine | |
IView view = | |
ViewEngines | |
.Engines | |
.FindPartialView(controllerContext, "RenderActionPartial") | |
.View; | |
// create a view context and assign the model | |
var viewContext = | |
new ViewContext( | |
controllerContext, | |
view, | |
new ViewDataDictionary(), | |
new TempDataDictionary(), | |
HttpContext.Current.Response.Output); | |
return viewContext; | |
} | |
} | |
internal class WebFormController : Controller | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simpler and clearer and works with MVC 3