Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting ASP.NET MVC into a WebForms page

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?

like image 752
Jose3d Avatar asked Nov 22 '10 11:11

Jose3d


People also ask

Can you mix Web Forms and MVC?

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.

Can we use ASPX page in MVC?

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.

Is ASP.NET Web Forms dead?

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.

When should I use ASP.NET MVC vs Web Forms?

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.

Is ASP NET MVC same as ASP NET Web Forms?

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.

What do WebForms and MVC have in common?

Both Asp.Net WebForms and Asp.Net MVC shares a common runtime and in specific, the request processing pipeline are also shared by both.

How to create a MVC application from an existing website?

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.

How to add a controller class to a web forms project?

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.


1 Answers

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>
like image 161
Darin Dimitrov Avatar answered Oct 20 '22 21:10

Darin Dimitrov