gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:46:48 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com