与 ASP.NET WebForms 相比,ASP.NET MVC 页面的“页面生命周期"是什么? [英] What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms?

查看:30
本文介绍了与 ASP.NET WebForms 相比,ASP.NET MVC 页面的“页面生命周期"是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与 ASP.NET WebForms 相比,ASP.NET MVC 页面的页面生命周期"是什么?

What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms?

我试图更好地理解这个简单"的问题,以确定我在一个(非常)简单的站点中的现有页面是否可以轻松地从 ASP.NET WebForms 转换.

I'm tryin to better understand this 'simple' question in order to determine whether or not existing pages I have in a (very) simple site can be easily converted from ASP.NET WebForms.

我正在寻找以下流程的转换"或替代生命周期.

Either a 'conversion' of the process below, or an alternative lifecycle would be what I'm looking for.

我目前在做什么:

(是的,我知道任何有能力回答我的问题的人都知道这一切——我只是想对生命周期"进行比较,所以我想我会先填写我们已经填写的内容都知道)

呈现页面:

  • 我有一个包含我的基本模板的母版页
  • 我有一些内容页面,这些页面为我提供了母版页中的命名区域,我将内容放入其中.
  • 在每个内容页面的事件处理程序中,我从数据库加载数据(主要是只读的).
  • 我将此数据绑定到表示网格、下拉列表或中继器的 ASP.NET 控件.这些数据都在生成的 HTML 中存在".其中一些会进入 ViewState(但我不会过多涉及!)
  • 我设置属性或将数据绑定到页面上的某些项目,例如 Image 或 TextBox 控件.
  • 页面被发送到客户端,呈现为不可重用的 HTML.
  • 我尽量避免使用除页面最低要求以外的 ViewState.

客户端(不使用 ASP.NET AJAX):

  • 我可能会使用 JQuery 和一些讨厌的技巧来查找页面上的控件并对其执行操作.
  • 如果用户从下拉列表中进行选择 - 会生成回发,在我的代码隐藏中触发 C# 事件.这个事件可能会进入数据库,但无论它做什么,一个全新生成的 HTML 页面最终都会被发送回客户端.
  • 我可以使用 Page.Session 来存储我以后需要重用的键值对

那么对于 MVC,这个生命周期"是如何改变的?

So with MVC how does this 'lifecycle' change?

推荐答案

我将尝试对您提到的每个要点进行评论:

I'll attempt to comment on each of the bullet points you mentioned:

您的母版页仍然存在于 MVC 中,用于为站点提供一致的布局.那里没有太多新东西.

Your master pages still exist in MVC and are used to provide a consistent layout to the site. not much new there.

您的内容页面将成为 MVC 世界中的视图.它们仍然为您的母版页提供相同的内容区域.

Your content pages will become views in the MVC world. They still provide the same content areas to your master pages.

不应在 MVC 中使用 webforms 的事件处理,而是您的 Controller 类及其操作方法将处理将您的数据加载到传递给视图的模型"中.

The eventhandling of webforms should not be used in MVC, instead your Controller classes and their action methods will handle loading your data into a "model" that gets passed to the view.

虽然在 MVC 中可以使用 webform 样式的数据绑定,但我发现它不是最佳解决方案.最好将您的数据放在模型类中并强类型您的视图,以便您可以直接访问该模型.然后只需使用 <%= ViewData.Model.SomeProperty %> 语法来访问您的数据并将其显示在所需的位置.至于视图状态,我的建议是忘记它甚至存在.

Although webform style databinding is possible in MVC, I find that it is not the optimal solution. Better to place your data in a model class and strongly type your view so that you have direct access to that model. Then its simply a matter of using the <%= ViewData.Model.SomeProperty %> syntax to access your data and display it in the desired locations. As for viewstate, my recommendation is to forget that it even exists.

请记住,使用 MVC 的优势之一是您可以控制发送给客户端的 HTML.拥抱这种力量并尝试找到可以让您保持这种控制的解决方案.Webform 控件试图对您隐藏 html,因此在您需要时更难自定义 html.

Remember that one of the advantages of using MVC is that you have control over the HTML you send to the client. Embrace that power and try to find solutions that allow you to maintain that control. Webform controls attempt to hide the the html from you and as such make it more difficult to customize the html when you need to.

我强烈推荐 JQuery 或其他类似的强大 javascript 库之一.但要学会使用它们直接访问 HTML DOM,避免 webform 控件的 id 重整问题.

I would highly recommend JQuery or one of the other similarly powerful javascript libraries. But learn to use them to access the HTML DOM directly and avoid the id mangling issues of webform controls.

您可以使用 jquery 挂钩到客户端的下拉选择并提交标准或 ajax 样式请求.这些请求可以返回新页面、重定向、html 片段甚至可用于更新现有页面的 JSON 数据.

You can use jquery to hook into the dropdown selection on the client side and submit standard or ajax style requests. Those request can return new pages, redirects, html fragments or even JSON data that can be used to update the existing page.

可以根据需要使用asp.net Session.

The asp.net Session can be used as needed.

这篇关于与 ASP.NET WebForms 相比,ASP.NET MVC 页面的“页面生命周期"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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