asp.net - Asp.net 页面生命周期和 Asp.net Mvc 页面生命周期有什么区别?

标签 asp.net asp.net-mvc

Asp.net页面生命周期有什么区别
和 Asp.net Mvc 页面生命周期?

Asp.net页面生命周期 只记得SILVER U
s-开始
I-初始化
L-负载
V- 验证
E-事件处理
R - 渲染
U -卸载
Mvc 和 Asp.net 页面的实际区别是什么?

最佳答案

ASP.NET 页面生命周期与 web 表单完全不同,没有像我们在 web 表单中那样的事件,例如:预渲染 , 初始化 等等,每当我们请求一个 URL 时,唯一发生的事情是,一个 Controller 的实例被创建并调用它的一些操作方法,这导致将 View 呈现为 HTML 作为浏览器中的响应。

ASP.NET MVC 页面生命周期:

根据微软 以下是asp.net mvc页面生命周期中涉及的主要步骤:

1) 路由

routes url to its controller and action

In ASP.NET application each asp.net page implements the IHTTPHandler interface.



此接口(interface)有一个 ProcessRequest() 方法,当您请求页面时会调用该方法。 ProcessRequest() 方法负责处理请求并生成响应。所以在 ASP.NET 应用程序中很简单,您在 url 中请求一个页面,如 http://mysite1\default.aspx然后它在磁盘上搜索该页面并执行 processrequest 方法并生成响应。

However in MVC application it doesn’t work in that way. There is no physical page exist for a particular request. All the requests are routed to a special class called Controller. The controller is responsible for generating the response and sending the content back to the browser.



2)Url路由模块拦截请求:

Whenever you make a request against an ASP.NET MVC application, the request is intercepted by the UrlRoutingModule HTTP Module.

When the UrlRoutingModule intercepts a request, the first thing the module does is to wrap up the current HttpContext in an HttpContextWrapper object.

The HttpContextWrapper object derives from HTTPContextBase class.



3)MVC处理程序执行

MVCHandler also inherit from the IHTTPAsyncHandler. When MVC Handler executes it will call the BeginProcessRequest method of the httpAsyncHandler asynchronously.

When the process request method is called a new controller gets created. The controller is created from a ControllerFactory. There is a ControllerBuilder Class which will set the ControllerFactory.

You can create your own ControllerFactory as well but by default it will be DefaultControllerFactory. The RequestContext and the name of the Contoller will be passed to the method CreateController Method to get the particular Contoller.



4) Controller 执行

Controller 被调用并且它的 Action 被用户请求调用。

The Execute() method starts by creating the TempData object. TempData is a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value.

The Execute() method gets the Action from the RouteData based on the URL.The Controller Class then call the ContollerActionInvoker that builds a list of parameters from the request.

These parameters, extracted from the request parameters, will act as method parameters.The parameters will be passed to whatever controller method gets executed.

Finally It will call the InvokeAction method to execute the Action.



5)渲染 View 方法调用

at last when we call reutrn View() Render View method is called and puts reponse on the page to be displayed.

The Controller typically either executes either the RedirectToAction Method or the RenderView Method. When you call a controller’s RenderView() method,the call is delegated to the current ViewEngine’s RenderView() method.

The WebFormViewEngine.RenderView() method uses a class named the ViewLocator class to find the view. Next, it uses a BuildManager to create an instance of a ViewPage class from its path.

Next, if the page has a master page, the location of the master page is set If the page has ViewData, the ViewData is set. Finally, the RenderView() method is called on the ViewPage.



摘要说明图:

enter image description here

深度图:

enter image description here

请求流"

这是 asp.net mvc 请求流程:

enter image description here

引用链接

详细了解请引用Understanding of MVC Page Life Cycle

还有 Here is another good article explaining MVC Page Life Cycle

关于asp.net - Asp.net 页面生命周期和 Asp.net Mvc 页面生命周期有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24690896/

相关文章:

asp.net-mvc - 字段子集的验证摘要

c# - SQL注入(inject)存储过程

asp.net - ASP.NET 应用程序中的 Google 桌面或索引文本文件搜索

asp.net - 如何验证ASP.NET MVC OutputCache是​​否可在服务器上正常工作?

asp.net-mvc - ASP.NET MVC - 将两个存储库返回到 View

javascript - 如何在 Ajax 错误上抛出 404 错误以定向到默认 404 行为?

asp.net-mvc - HttpErrors existingResponse ="Replace"阻止了 MVC 的自定义错误处理

ASP.NET 动态命令按钮事件未触发

ASP.NET MVC : Returning large amounts of data from FileResult

asp.net-mvc - ASP.Net MVC RC2 ValidationMessage 和表单字段冲突?