ASP的“页面生命周期”是什么?NET MVC页面,与ASP相比。净WebForms吗?

时间:2021-12-10 03:23:30

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

ASP的“页面生命周期”是什么?NET MVC页面,与ASP相比。净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.

我试图更好地理解这个“简单”问题,以便确定我在一个(非常)简单的站点中拥有的现有页面是否可以从ASP中轻松转换。净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!)
  • 我将这些数据绑定到ASP。表示网格、下拉或中继器的NET控件。这些数据在生成的HTML中都“存在”。有些会进入ViewState(但我不会讲太多!)
  • 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.
  • 页面被发送到呈现为不可重用HTML的客户端。
  • I try to avoid using ViewState other than what the page needs as a minimum.
  • 我尽量避免使用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.
  • 我可能会使用JQuery和一些讨厌的技巧在页面上找到控件并对其执行操作。
  • 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.
  • 如果用户从下拉菜单中选择——生成一个回发,在我的代码后面触发一个c#事件。这个事件可能会转到数据库,但是不管它做什么,一个完全新生成的HTML页面最终都会被发送回客户端。
  • I may use Page.Session to store key value pairs I need to reuse later
  • 我可以使用页面。会话来存储稍后需要重用的键值对

So with MVC how does this 'lifecycle' change?

那么对于MVC,这种“生命周期”是如何变化的呢?

1 个解决方案

#1


36  

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中使用,相反,您的控制器类和它们的操作方法将处理如何将数据加载到一个传递给视图的“模型”中。

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.

虽然webform风格的数据库在MVC中是可行的,但是我发现它并不是最佳的解决方案。最好将数据放置在一个模型类中,并对视图进行强类型输入,以便能够直接访问该模型。然后,这只是使用<%= ViewData.Model的问题。SomeProperty %>语法访问您的数据并将其显示在所需的位置。至于viewstate,我的建议是忘记它的存在。

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会话。

#1


36  

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中使用,相反,您的控制器类和它们的操作方法将处理如何将数据加载到一个传递给视图的“模型”中。

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.

虽然webform风格的数据库在MVC中是可行的,但是我发现它并不是最佳的解决方案。最好将数据放置在一个模型类中,并对视图进行强类型输入,以便能够直接访问该模型。然后,这只是使用<%= ViewData.Model的问题。SomeProperty %>语法访问您的数据并将其显示在所需的位置。至于viewstate,我的建议是忘记它的存在。

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会话。