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

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

问题描述

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

我试着更好地理解这个简单的问题,以确定是否我有一个非常简单的网站可以从ASP.NET的WebForms很容易地转换现有页面。

无论是在下面的过程中,或另一种生命周期的转化将是什么我要找的。

目前我在做什么:

(是的,我知道,已经能回答我的问题的人都知道这一切 - 我只是试着得到'生命周期'的比较,所以我想我会在我们已经开始填充都知道)

渲染页面:


  • 我有一个包含我的基本模板的母版页

  • 我有给我的名字命名的区域从主页进我把内容内容页。

  • 在每个内容页面我从数据库中加载数据的事件处理程序(主要是只读)。

  • 我绑定此数据ASP.NET控件重新presenting网格,下拉菜单或中继器。这些数据都住生成的HTML中。其中一些进入视图状态(但我不会进入太多!)

  • 我设置属性和数据绑定到特定的项目,如在页面上的图像或文本框控件。

  • 页面被发送到渲染为不可重用的HTML客户端。

  • 我尽量避免使用比页面需要最低限度的ViewState其他

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


  • 我可以用jQuery和一些讨厌的技巧,找到页面上的控件并对其进行操作。

  • 如果用户从下拉选择 - 产生一个回发这在我的codebehind触发一个C#的事件。此事件可能会到数据库中,但无论它做一个完全新生成的HTML页面最终得到发送回客户端。

  • 我可以用Page.Session存储键值对我以后需要再次使用

因此​​,与MVC如何做这个'生命周期'的变化?


解决方案

我会尝试在每个你所提到的要点的评论:

您主页仍然存在于MVC和用于提供一个一致的布局的网站。没有多少新东西。

您内容页面将成为意见中的MVC世界。他们还提供了相同的内容领域的母版页。

Web表单的eventhandling不应在MVC中使用,而不是你的控制器类和它们的动作方法将处理您的数据加载到获取传递给视图的样板。

虽然Web窗体风格绑定在MVC是可能的,我觉得这是不是最佳的解决方案。最好将你的数据模型类,并强烈键入您的视图,让你有这种模式的直接访问。然后它只需使用&LT的问题;%= ViewData.Model.SomeProperty%GT; 语法来访问您的数据并显示在需要的位置。至于视图状态,我的建议是要忘记它的存在。

请记住,使用MVC的优点之一是,你必须对你发送给客户端HTML控件。拥抱权力,并试图找到解决办法,让你保持这种控制。网络表单控件试图从你隐藏的HTML,因此更难以自定义HTML,当您需要。

我会强烈建议JQuery的或其他类似的强大的JavaScript库之一。但是,学会利用它们来直接访问HTML DOM,避免ID重整表单控件的问题。

您可以使用jQuery挂钩到客户端上的下拉菜单选择,并提交标准或AJAX风格的请求。这些要求可以返回新页面,重定向,HTML片段甚至是JSON数据可用于更新现有的页面。

的asp.net会话可以根据需要使用。

What is the 'page lifecycle' of an ASP.NET MVC page, compared to 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.

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.

Client side (not using 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

So with MVC how does this 'lifecycle' change?

解决方案

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.

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

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.

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.

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.

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.

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.

The asp.net Session can be used as needed.

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

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