Is there any way to render a normal view of asp.net MVC action onto an old .aspx
WebForm using an MVC helper or some other method?
Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.
If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.
Does this mean ASP.NET Web Forms is dead and should no longer be used? Of course not! As long as the . NET Framework ships as part of Windows, ASP.NET Web Forms will be a supported framework.
Asp.Net MVC has Partial Views for code re-usability. Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development.
However, it is completely separated from ASP.NET MVC. And, just because of this, it is completely separated from the ASP.NET runtime too. ASP.NET Web Forms and ASP.NET MVC share the same runtime environment and requests are routed through exactly the same pipeline.
Both Asp.Net WebForms and Asp.Net MVC shares a common runtime and in specific, the request processing pipeline are also shared by both.
Create a new ASP.NET MVC 4 Application and selected the Empty Template (with Razor) Once loaded, right-clicked References > Browse and then navigated to the Bin folder of my existing website.
You can open an existing Web Forms project and then just choose to add a controller class. When you do so, Visual Studio 2013 just adds the full set of dependencies for ASP.NET MVC and you’re up and running in a matter of seconds. Note that Visual Studio offers the same nice service also if you add a Web API controller to a Web Forms project.
Disclaimer: The proposed solution below is a hack.
Suppose that you have the following controller:
public class HomeController : Controller
{
[ChildActionOnly]
public ActionResult Foo()
{
return View();
}
}
and a corresponding partial (Foo.ascx
).
And now in a legacy WebForms page you wanted to use this action. Here's how you could proceed:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script type="text/C#" runat="server">
private class DummyController : Controller { }
public HtmlHelper Html { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
var httpContext = new HttpContextWrapper(Context);
var controllerContext = new ControllerContext(
httpContext,
new RouteData(),
new DummyController()
);
var viewContext = new ViewContext(
controllerContext,
new WebFormView("Views"),
new ViewDataDictionary(),
new TempDataDictionary(),
TextWriter.Null
);
Html = new HtmlHelper(viewContext, new ViewPage());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= Html.Action("About", "Home") %>
</div>
</form>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With