开发者

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

开发者 https://www.devze.com 2023-01-14 06:01 出处:网络
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 a

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.开发者_如何学JAVAHelperMethod %>

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?


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消