zoukankan      html  css  js  c++  java
  • 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm实际上共用一套ASP.NET请求管道

    .NET FrameWork4在系统全局配置文件(如在如下目录中C:WindowsMicrosoft.NETFramework64v4.0.30319Config)

    中添加了一个名字叫UrlRoutingModule的HttpModule


    通过反编译工具我们可以看见

    UrlRoutingModule是在System.Web程序集下,并不是在Mvc程序集里面,本身在.NETFrameWork框架中就有这个路由类,同时实现IHttpModule接口,那么它肯定有个Init方法.


    可以看到它往我们的请求application对象,也就是我们的请求管道的第7个事件(PostResolveRequestCache

    )上注册了一个方法.如下

     

     1 public virtual void PostResolveRequestCache(HttpContextBasecontext)
     2 
     3 {
     4 
     5     RouteData routeData= this.RouteCollection.GetRouteData(context);
     6 
     7    if (routeData != null)
     8 
     9     {
    10 
    11        IRouteHandler routeHandler = routeData.RouteHandler;
    12 
    13        if (routeHandler == null)
    14 
    15        {
    16 
    17            throw newInvalidOperationException(string.Format(CultureInfo.CurrentCulture,SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[0]));
    18 
    19        }
    20 
    21        if (!(routeHandler is StopRoutingHandler))
    22 
    23        {
    24 
    25            RequestContext requestContext = new RequestContext(context, routeData);
    26 
    27            context.Request.RequestContext = requestContext;
    28 
    29            IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
    30 
    31            if (httpHandler == null)
    32 
    33            {
    34 
    35                 object[] args = new object[] {routeHandler.GetType() };
    36 
    37                 throw newInvalidOperationException(string.Format(CultureInfo.CurrentUICulture,SR.GetString("UrlRoutingModule_NoHttpHandler"), args));
    38 
    39            }
    40 
    41            if (httpHandler is UrlAuthFailureHandler)
    42 
    43            {
    44 
    45                 if(!FormsAuthenticationModule.FormsAuthRequired)
    46 
    47                 {
    48 
    49                     throw newHttpException(0x191, SR.GetString("Assess_Denied_Description3"));
    50 
    51                 }
    52 
    53                UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current,this);
    54 
    55            }
    56 
    57            else
    58 
    59            {
    60 
    61                context.RemapHandler(httpHandler);
    62 
    63            }
    64 
    65        }
    66 
    67     }
    68 
    69 }
    View Code

    我们可以看到这个方法一开始就使用了静态路由表中的的数据,而这个静态路由表数据正是我们在Global.asax中的Application_Start中向静态路由表中注册的路由数据(Mvc网站在第一次运行就会执行Application_Start,这个时候就把路由数据注册到了这个RouteTable里面了)


    ASPNET_ISAPI.dll中的HttpApplicationFactory类创建HttpApplication对象的时候,率先会去检查Application_Start是否被调用了,如果没被调用它就会去执行一次

    接着获取Global文件里的类型作为网站的HttpApplication,每次返回一个HttpApplication类对象或者子类的对象,读取配置文件,创建系统配置文件及用户配置的HttpModule对象,

    循环调用Init方法,为application对象里面的某些事件注册方法(即向请求管道里的时间注册用户的代码)

     

    RouteData routeData =this.RouteCollection.GetRouteData(context);

    根据上下文中的Url去路由表中匹配所有的路由规则,匹配了就返回一个路由对象.

    如果路由对象等于null,那么什么都不干.也就是就算是普通的aspx也会走这里,只不过根据aspx路径是匹配不到路由的.那么此时第7个事件是什么都不干的,接着走第八个事件

    如果请求的是aspx,在第8个事件就创建页面类对象.

    如果找到了路由匹配,那么第7个事件就会创建一个MvcHandler存在HttpContext上下文的RemapHandler中,接着第8个事件判断一下上下文是否有MvcHandler这个对象,如果有的话就不做任何事情,如果没有那就证明不是mvc网站,就会根据url的后缀去创建请求的页面类对象.所以asp.net webform和asp.net mvc共用同一套.net框架

    版权声明:本文为博主原创文章,转载请注明详细来源。

  • 相关阅读:
    剑指offer——最小的K个数和数组中第K大的元素
    Leetcode刷题指南链接整理
    160. Intersection of Two Linked Lists
    100. Same Tree
    92. Reverse Linked List II
    94. Binary Tree Inorder Traversal
    79. Word Search
    78,90,Subsets,46,47,Permutations,39,40 DFS 大合集
    0x16 Tire之最大的异或对
    0x16 Tire
  • 原文地址:https://www.cnblogs.com/zhangyihui/p/4801947.html
Copyright © 2011-2022 走看看