Asp.net Web Api 路由匹配

using System.Web.Mvc;
using System.Web.Routing;

namespace KingT.Web.TimeLine
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "TimeLine",
                url: "tl/{visitId}/{from}/{to}",
                defaults: new { controller = "Home", action = "Index", from = UrlParameter.Optional, to = UrlParameter.Optional }
            );
       //如果能匹配到这一个就不再向下匹配 
            routes.MapRoute(
                name: "TimeLineSetting",
                url: "setting",
                defaults: new { controller = "Home", action = "Setting" }
            );

            routes.MapRoute(
                name: "TimeLineSettingItem",
                url: "setting/{item}",
                defaults: new { controller = "Home", action = "SettingItem" }
            );

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

http://localhost:8099/tl/2015122800143/2015-12-24/2016-1-28 可以匹配第一个、

routes.MapRoute(
                name: "TimeLine",
                url: "{controller}/{action}/tl/{visitId}/{from}/{to}",
                defaults: new { controller = "Home", action = "Index", from = UrlParameter.Optional, to = UrlParameter.Optional }
            );

http://localhost:8099/Home/Index/tl/2015122800143/2015-12-24/2016-1-28 可以匹配上面的路由

posted @ 2017-05-25 15:14  阿波  阅读(239)  评论(0编辑  收藏  举报