ASP.NET 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基類,如下:

路由能提供什麼?

● 通過路由的GetRouteData()方法,獲取到封裝路由信息的RouteData類型;
● 通過路由的Constraints屬性獲取路由約束;
……

MapRoute()是RouteCollection的擴展方法:

RouteCollection是什麼?

RouteCollection是RouteBase的集合類。

2、註冊路由規則

在全局文件中註冊路由規則:

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

原來,所有的路由規則都存放在RouteTable中類型為RouteCollection的Routes屬性中瞭。

3、UrlRoutingModule處理url請求,輸出MvcHandler以處理HTTP請求

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);
    }
}

以上就是這篇文章的全部內容瞭,希望本文的內容對大傢的學習或者工作具有一定的參考學習價值,謝謝大傢對WalkonNet的支持。如果你想瞭解更多相關內容請查看下面相關鏈接

推薦閱讀: