Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ASP.Net MVC View inside WebForms .aspx page?

I have a large web application that I have set up as both ASP.Net Web Forms and MVC. Some of the newer pages are MVC (.mvc) and some are Web Forms (.aspx). Since this is a business app and functions are being constantly added, it's hard for me to have the time to stop and replace existing functionality to convert it over. So I would like to do it piece by piece, control by control.

To do this, all I would need to be able to do is to replace certain controls with

<% Html.RenderPartial() %> or <% Html.HelperMethod %>

Is it possible to include namespaces or inherit the page from something to allow these methods to be used, while still maintaining the rest of the Page life cycle for other controls on the page?

like image 523
Jared Avatar asked Sep 01 '10 19:09

Jared


People also ask

Can we have ASPX page in MVC?

Answers. You don't need to add a special route to add a webforms . aspx page to your MVC applications. Just add the file and it will work as long as you don't put it in the Views folder.

Can you mix MVC and webforms?

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. There's actually only one crucial difference: ASP.NET lives in System.

How do you render a partial view inside a view in MVC?

Partial function which renders the Partial View. The name of the View and the object of the CustomerModel class are passed to the @Html. Partial function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


1 Answers

I saw this question and I remembered that I've read a few posts by Scott Hanselman regarding this, so here you go:

  • Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications
  • ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side

Also two more articles on the subject:

  • http://weblogs.asp.net/rajbk/archive/2010/05/11/running-asp-net-webforms-and-asp-net-mvc-side-by-side.aspx
  • http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

I think to summarize you can:

  • Have ASP.NET MVC and ASP.NET WebForms side-by-side, such as that you can have the new views in ASP.NET MVC and the old ones will remain in WebForms (if it works why rewrite it?). You "just" need to figure out how to share the master page.
  • You can use basic WebForms controls inside non-Razor ASP.NET MVC views, but if they use PostBacks or require ViewState - they won't work, because the full WebForms lifecycle isn't there in ASP.NET MVC views.
  • Regarding partial views inside WebForms. Uou can hack/force the ASP.NET MVC pipeline to render a route/view to a string and append that to your WebForms page. The problem and question however is how useful is that since you will loose the model binding during POST as well as MVC model validation if you are doing edits. Some code to render a view - How to include a partial view inside a webform . You can also always pull the partial view using ajax if need be.
like image 95
Ivan Zlatev Avatar answered Nov 09 '22 09:11

Ivan Zlatev