如何在 WebForms .aspx 页面中使用 ASP.Net MVC 视图? [英] How to use ASP.Net MVC View inside WebForms .aspx page?

查看:18
本文介绍了如何在 WebForms .aspx 页面中使用 ASP.Net MVC 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型 Web 应用程序,我将它设置为 ASP.Net Web 窗体和 MVC.一些较新的页面是 MVC (.mvc),一些是 Web Forms (.aspx).由于这是一个商业应用程序,而且功能在不断添加,我很难有时间停下来替换现有的功能来转换它.所以我想一块一块地,一个一个地控制.

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() %><% 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?

推荐答案

我看到了这个问题,我记得我读过 Scott Hanselman 写的一些关于这个的帖子,所以你去:

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

还有两篇关于这个主题的文章:

Also two more articles on the subject:

我想总结一下你可以:

  • 同时拥有 ASP.NET MVC 和 ASP.NET WebForms,例如您可以在 ASP.NET MVC 中拥有新视图,而旧视图将保留在 WebForms 中(如果它有效,为什么要重写它?).您只需要"弄清楚如何共享母版页.
  • 您可以在非 Razor ASP.NET MVC 视图中使用基本的 WebForms 控件,但如果它们使用 PostBacks 或需要 ViewState - 它们将无法工作,因为完整的 WebForms 生命周期不在 ASP.NET MVC 视图中.
  • 关于 WebForms 中的部分视图.您可以破解/强制 ASP.NET MVC 管道将路由/视图呈现为字符串并将其附加到您的 WebForms 页面.然而,问题和问题是,因为如果您进行编辑,您将在 POST 以及 MVC 模型验证期间丢失模型绑定,所以它有多大用处.一些渲染视图的代码 - 如何在其中包含局部视图一个网络表单.如果需要,您也可以随时使用 ajax 拉取局部视图.
  • 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.

这篇关于如何在 WebForms .aspx 页面中使用 ASP.Net MVC 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆