码迷,mamicode.com
首页 > Web开发 > 详细

MVC路由之浅见

时间:2014-07-01 19:58:19      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   width   

  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基类,如下:
bubuko.com,布布扣

 

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

 

MapRoute()是RouteCollection的扩展方法:
bubuko.com,布布扣

 

RouteCollection是什么?
RouteCollection是RouteBase的集合类。
bubuko.com,布布扣

 

  2、注册路由规则

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

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

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

 

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

bubuko.com,布布扣

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路由之浅见

标签:style   blog   http   color   使用   width   

原文地址:http://www.cnblogs.com/darrenji/p/3816945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!