用路由設定,記錄下

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   io   

 routes.MapRoute(                   "Default_a",                   "huhangfei/{pageindex}-{state}-{size}.html",                     new { controller = "Home", action = "Index", pageindex = 1, state = 0 ,size=0},                    new { pageindex = @"\d+", state = @"\d+", size = @"\d+" }                );            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );

大家在學習MVC的過程,老是用到設定路由,但有6個常用路由,是大家經常用到的。

一.預設路由(MVC內建)

public static void RegisterRoutes(RouteCollection routes) {     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");      routes.MapRoute(         "Default", // 路由名稱         "{controller}/{action}/{id}", // 帶有參數的 URL         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值 (UrlParameter.Optional-可選的意思)    );   } 


二.不帶參數的路由

 routes.MapRoute("NoParameter", "{controller}/{action}/{id}");

三.帶命名空間的路由

 routes.MapRoute(              "AdminControllers", // 路由名稱              "{controller}/{id}-{action}", // 帶有參數的 URL              new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 參數預設值              new string[] { "Admin.Controllers" }//命名空間          );

四.帶約束的路由規則(約束的意思就是用正則這類約束必須符合條件才可以)

routes.MapRoute(                "RuleControllers",                "{controller}/{action}-{Year}-{Month}-{Day}}",                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },                new { Year = @"^\d{4}", Month = @"\d{2}" } //4位元 2位元            );

五.帶名稱空間,帶約束,帶預設值的路由規則

 routes.MapRoute(                "Rule1",                "Admin/{controller}/{action}-{Year}-{Month}-{Day}",                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },                new { Year = @"^\d{4}", Month = @"\d{2}" },                new string[] { "Admin.Controllers" }            );

六.捕獲所有的路由

  routes.MapRoute(                "All", // 路由名稱                "{*Vauler}", // 帶有參數的 URL                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值            );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.