MVC路由之浅见

  1、定义路由、路由规则

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

路由是什么?
Route派生于RouteBase基类,如下:
MVC路由之浅见

 

路由能提供什么?
● 通过路由的GetRouteData()方法,获取到封装路由信息的RouteData类型;
● 通过路由的Constraints属性获取路由约束;
......

 

MapRoute()是RouteCollection的扩展方法:
MVC路由之浅见

 

RouteCollection是什么?
RouteCollection是RouteBase的集合类。
MVC路由之浅见

 

  2、注册路由规则

在全局文件中注册路由规则:

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ......
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            ......
        }
    }

原来,所有的路由规则都存放在RouteTable中类型为RouteCollection的Routes属性中了。

 

  3、UrlRoutingModule处理url请求,输出MvcHandler以处理HTTP请求

MVC路由之浅见

UrlRoutingModule实现IHttpModule接口,对HttpApplication的PostResolveRequest事件进行侦听:

public class UrlRoutingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PostResolveRequestCache += OnPostResolveRequestCache;
    }

    portected virtual void OnPostResolveRequestCache(object sender, EventArgs e)
    {
        //把当前上下文封装
        HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);

        //获取当前请求中的RouteData类型
        RouteData routeData = RouteTable.Routes.GetRouteData(httpContext);
        if(routeData == null)
        {
            return;
        }

        //把routeData再封装起来
        RequestContext requestContext = new RequestContext{
            RouteData = routeData,
            HttpContext = httpContext
        };

        //从RouteData的RouteHandler属性获取IRouteHandler类型
        //IRouteHandler的GetHttpHandler()方法获取IHttpHandler
        IHttpHandler handler = routeData.RouteHandler.GetHttpHandler(requestContext);

        //为当前上下文设置HttpHandler,用于对当前HTTP请求的处理
        httpContext.RemapHandler(handler);
    }
}

IRouteHandler是什么?
他的唯一职责是生产IHttpHandler实例。

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

在MVC中默认使用的是MvcRouteHandler,是IRouteHandler的实现,返回类型为IHttpHandler的MvcHandler:

public class MvcRouteHandler : IRouteHander
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MvcHandler(requestContext);
    }
}

 

MvcHandler是MVC中IHttpHandler的默认实现:

public class MvcHandler : IHttpHandler
{
    publci bool IsReusable{get {return false;}}

    public RequestContext RequestContext{get; private set;}

    public MvcHandler(RequestContext requestContext)
    {
        this.RequestContext = requestContext;
    }

    public void ProcessRequest(HttpContext context)
    {
        string controllerName = this.RequestContext.RouteData.Controller;
        IControllerFactory controllerFactory = COntrollerBuilder.Current.GetControllerFactory();
        IController controller = controllerFactory.CreateController(this.RequestContext, controllerName);
        controller.execute(this.RequestContext);
    }
}

MVC路由之浅见,布布扣,bubuko.com

MVC路由之浅见

上一篇:MS Sql Service 记一次in查询的优化


下一篇:SQL如何查询出某一列中不同值出现的次数?