asp.net-mvc 与 ASP.NET WebForms 相比,ASP.NET MVC 页面的“页面生命周期”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/460145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms?
提问by Simon_Weaver
What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms?
与 ASP.NET WebForms 相比,ASP.NET MVC 页面的“页面生命周期”是什么?
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.
我试图更好地理解这个“简单”的问题,以确定我在(非常)简单的站点中拥有的现有页面是否可以轻松地从 ASP.NET WebForms 转换。
Either a 'conversion' of the process below, or an alternative lifecycle would be what I'm looking for.
我正在寻找以下过程的“转换”或替代生命周期。
What I'm currently doing:
我目前在做什么:
(yes i know that anyone capable of answering my question already knows all this -- i'm just tryin to get a comparison of the 'lifecycle' so i thought i'd start by filling in what we already all know)
(是的,我知道任何能够回答我的问题的人都已经知道这一切——我只是想对“生命周期”进行比较,所以我想我会先填写我们都知道的内容)
Rendering the page:
渲染页面:
- I have a master page which contains my basic template
- I have content pages that give me named regions from the master page into which I put content.
- In an event handler for each content page I load data from the database (mostly read-only).
- I bind this data to ASP.NET controls representing grids, dropdowns or repeaters. This data all 'lives' inside the HTML generated. Some of it gets into ViewState (but I wont go into that too much!)
- I set properties or bind data to certain items like Image or TextBox controls on the page.
- The page gets sent to the client rendered as non-reusable HTML.
- I try to avoid using ViewState other than what the page needs as a minimum.
- 我有一个包含我的基本模板的母版页
- 我有内容页面,这些页面从我放置内容的母版页中为我提供命名区域。
- 在每个内容页面的事件处理程序中,我从数据库加载数据(主要是只读的)。
- 我将此数据绑定到表示网格、下拉列表或中继器的 ASP.NET 控件。这些数据都在生成的 HTML 中“存在”。其中一些进入 ViewState(但我不会过多地讨论!)
- 我设置属性或将数据绑定到页面上的某些项目,如 Image 或 TextBox 控件。
- 该页面被发送到呈现为不可重用 HTML 的客户端。
- 我尽量避免使用除页面所需之外的 ViewState 作为最低要求。
Client side (not using ASP.NET AJAX):
客户端(不使用 ASP.NET AJAX):
- I may use JQuery and some nasty tricks to find controls on the page and perform operations on them.
- If the user selects from a dropdown -- a postback is generated which triggers a C# event in my codebehind. This event may go to the database, but whatever it does a completely newly generated HTML page ends up getting sent back to the client.
- I may use Page.Session to store key value pairs I need to reuse later
- 我可能会使用 JQuery 和一些讨厌的技巧来查找页面上的控件并对其执行操作。
- 如果用户从下拉列表中进行选择 - 会生成一个回发,它会在我的代码隐藏中触发 C# 事件。这个事件可能会进入数据库,但无论它做什么,一个全新生成的 HTML 页面最终都会被发送回客户端。
- 我可以使用 Page.Session 来存储我以后需要重用的键值对
So with MVC how does this 'lifecycle' change?
那么对于 MVC,这个“生命周期”是如何改变的呢?
回答by Mike Glenn
I'll attempt to comment on each of the bullet points you mentioned:
我将尝试对您提到的每个要点发表评论:
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 世界中的视图。它们仍然为您的母版页提供相同的内容区域。
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.
Webforms 的事件处理不应在 MVC 中使用,而是您的 Controller 类及其操作方法将处理将您的数据加载到传递给视图的“模型”中。
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 中可以使用 webform 样式的数据绑定,但我发现它不是最佳解决方案。最好将您的数据放在模型类中并强类型化您的视图,以便您可以直接访问该模型。然后它只是使用<%= ViewData.Model.SomeProperty %>语法来访问您的数据并将其显示在所需位置的问题。至于视图状态,我的建议是忘记它甚至存在。
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.
请记住,使用 MVC 的优势之一是您可以控制发送给客户端的 HTML。拥抱这种力量并尝试找到可以让您保持这种控制的解决方案。Webform 控件试图对您隐藏 html,因此在您需要时自定义 html 变得更加困难。
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 或其他类似强大的 javascript 库之一。但是学习使用它们直接访问 HTML DOM 并避免 webform 控件的 id 修改问题。
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.
您可以使用 jquery 挂钩到客户端的下拉选择并提交标准或 ajax 样式请求。这些请求可以返回新页面、重定向、html 片段甚至可用于更新现有页面的 JSON 数据。
The asp.net Session can be used as needed.
可以根据需要使用asp.net Session。

