惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
I
InfoQ
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
MongoDB | Blog
MongoDB | Blog
T
Tor Project blog
The Register - Security
The Register - Security
H
Help Net Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
B
Blog RSS Feed
Latest news
Latest news
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
C
Cisco Blogs
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
S
Schneier on Security
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
W
WeLiveSecurity

博客园 - DarroldYang

linux 多线程,锁同步 Saas模式数据库层数据架构以及数据删除处理 ASP.NET MVC 自定义过滤属性实现Enterprise的log功能 ASP.NET MVC HtmlHelper类的方法总结 - DarroldYang (Windows 7 IIS 7.5里面的 IIS and Built-in Accounts) IIS和内置帐户的IIS 用户控件,事件的触发顺序 IIS7.0 An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode 转ORACLE函数大全 ASP.NET AJAX 调用Service Method (Too simply) Office编程知识点汇总(3)--格式设置 Office编程知识点汇总(2)--基本工作表任务 Office编程知识点汇总:基本工作簿任务 asp.NET应用程序的技巧 CrystalReport的导出“Push”方式 Ajax Control Toolkit MutuallyExclusiveCheckBox 服务器端控件 Ajax Control Toolkit Slider 服务器端控件 Ajax Control Toolkit AlwaysVisibleControl 服务器端控件 Ajax Control Toolkit Accordion 服务器端控件 判断代码执行环境
ASP.NET MVC 请求生命周期
DarroldYang · 2010-04-02 · via 博客园 - DarroldYang

       当一个asp.net mvc应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在asp.net mvc应用程序Http request
和Http response 过程中,主要包含8个步骤:
     1)RouteTable(路由表)的创建
     2)UrlRoutingModule 请求拦截
     3)Routing engine 确定route
     4)route handler 创建相关的IHttpHandler实例
     5)IHttpHandler实例确定Controller(控制器)
     6)Controller执行
     7)一个视图引擎创建
     8) 视图呈现
主要流程图如下:

              

1)RouteTable的创建
       RouteTable的创建发生在mvc应用程序的启动 或者web应用程序池的重启!通常的asp.net程序,一个页面请求对应磁盘上的一个页面!如(http://localhost/index.aspx
 对应到服务器磁盘上的文件index.aspx)index.aspx实际上是一个类,由IHttpHandler创建实例化。IHttpHandler包含一个
 ProcessRequest方法,负责响应页面输出!

但是mvc application 是不同的,每一个请求映射到route,route 定义在route table,在应用程序启动时创建!

RouteTable的在应用程序的具体使用如下

代码

  public class MvcApplication : System.Web.HttpApplication
    {
        
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute(
"{resource}.axd/{*pathInfo}");

            routes.MapRoute(

"Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
            routes.MapRoute(
               
"Account",                                              // Route name
               "{controller}/{action}/{id}",                           // URL with parameters
               new { controller = "Account", action = "LogOn", id = "" }  // Parameter defaults
           );
       
        }
protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

2)UrlRoutingModule 请求拦截
 每一个Http 请求 都被UrlRoutingModule拦截,UrlRoutingModule提供了当前的HttpContext的routing engine(路由引擎)。HttpContext实例包含当前请求的所有数据。UrlRoutingModule控制着routing engine,提供了HttpContext数据到routing engine! UrlRoutingModule实现了IHttpModule接口,在web.config 文件进行了注册!

UrlRoutingModule 具体的数据结构如下:

代码

   
 
public class UrlRoutingModule : IHttpModule
   {
    
// 主要的 Methods
    protected virtual void Init(HttpApplication application);
    
private void OnApplicationPostMapRequestHandler(object sender, EventArgs e);
    
private void OnApplicationPostResolveRequestCache(object sender, EventArgs e);
    
public virtual void PostMapRequestHandler(HttpContextBase context);
    
public virtual void PostResolveRequestCache(HttpContextBase context);
    
void IHttpModule.Init(HttpApplication application);// Properties
    public RouteCollection RouteCollection { getset; }

    }
     UrlRoutingModule 在WebConfig的注册

<httpModules>
    
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0,                  Culture=neutral,    PublicKeyToken=31BF3856AD364E35"/>
        
</httpModules>

3)Routing engine 确定route
 routing engine基于当前HttpContext确定Route的处理。routing engine 指出route table里面匹配的route ,并在IRouteHandler实例创建route处理!

4)route handler 创建相关的IHttpHandler实例
 在route table里,每一个route 都与一个IHttpHandler对应。IHttpHandler基于当前的HttpContext数据负责创建一个Controller(控制器)!IHttpHandler是由当前活动的IRouteHandler的GetHttpHandler所创建!

具体的细节如下

public interface IRouteHandler
{
    
// Methods
    IHttpHandler GetHttpHandler(RequestContext requestContext);
}

5)IHttpHandler实例确定Controller(控制器)
 在MVC应用程序中,MvcHandler实现了IHttpHandler,Controller实例,是基于所输入的HttpContext 和Url参数与route 对应的,ControllerFactory 创建一个controller,ControllerContext包含上下文数据,传入到controller的Excute方法,触发controller的逻辑处理!

MvcHandler主要有一个ControllerBuilder  _controllerBuilder字段;

具体细节如下

代码

public class MvcHandler : IHttpAsyncHandler, IHttpHandler, IRequiresSessionState
{
    
// 主要的Fields
    private ControllerBuilder _controllerBuilder;
}
ControllerBuilder类主要有一个方法GetControllerFactory
public class ControllerBuilder
{
  
public IControllerFactory GetControllerFactory();
}通过实现IControllerFactory 工厂 创建一个Controller

6)Controller执行
 所有的controller 逻辑调用执行时,actions请求被执行!当controller的逻辑被执行时,会返回一个ActionResult。一个ActionResult实例,会触发呈现一个View(视图),当触发发生时,一个视图引擎被创建,进行进一步的处理

7)一个视图引擎创建
 视图引擎实例会创建一个IView接口实例,返回一个ViewEngineResult实例,

8) 视图呈现
 IView实例编译请求视图,提供Render方法调用的数据!