路由解析過程:
這是routing元件的兩個重要的作用!
例項一:系統預設提供的路由格式,下面是系統給的預設**:
url格式為:http://localhost:80/public static void registerroutes(routecollection routes)
2 .axd/");
4 5 routes.maproute(
6 "default", // 路由名稱
7 "//", // 帶有引數的 url
8 new // 引數預設值
9 );
10 }
home/index
對應規則為:
// 黑體部分就是對應部分。這還是有預設值的情況。
詳細匹配應該為:http://localhost:80/
custom/detials/1
。去掉主機網域名稱,剩下的對應就是匹配controller和actiong了。通過routing元件解析這個url,controller是custom,action是detials。傳遞過去的id是1。這就是使用了maproute( string name, string url, object defaults);這個方法的過載。
例項二:不使用預設值的url路由規則
函式簽名:
maproute( string name, string url);
routes.maproute("沒有預設值路由規則", "/-");
適合的url例子:http://localhost:80/custom/1-detials
它將不匹配http://localhost:80/
例項三:帶命名空間的url路由規則
函式簽名:maproute( string name, string url, string namespaces);//路由名,url規則,命名空間
routes.maproute(
2 "myurl", // 路由名稱
3 "/-", // 帶有引數的 url
4 new , // 引數預設值
5 new string //命名空間
這個例子是帶命名空間的路由規則,這在aeras使用時非常有用。
例項四:帶約束的路由規則
函式簽名:
maproute( string name, string url, object defaults, object constraints);//路由名,url規則,預設值,命名空間
routes.maproute(
2 "rule1",
3 "/---}",
4 new ,
5 new ", month = @"\d" }
6 );
複製**
例項五:帶命名空間,帶約束,帶預設值的路由規則
函式簽名:
例項六:捕獲所有的路由
routes.maproute(
2 "all", // 路由名稱
3 "", // 帶有引數的 url
4 new // 引數預設值
5 );
routes.ignoreroute(".axd/");//是忽略這個規則的url
arearegistration.registerallareas();//註冊所有的areas
2 registerroutes(routetable.routes);//註冊自定義規則
3 5
MVC多路由規則配置
public static void registerroutes routecollection routes axd routes.maproute default 路由名稱 帶有引數的 url new 引數預設值 routes.maproute willian new global檔案中寫入自...
MVC 深入講解Routing 路由規則 八
一 客戶端 控制器 在專案中我們引用了system.web.routing,如果第乙個匹配成功了,那麼後面的都不會再匹配。1.routing的作用 確定colltroller,確定action,確定其他引數,根據識別出來的資料,將請求傳遞給controller和action 2.routing規則有...
MVC 路由 概述
設定url路由 url路徑中,大括號 內部的代表引數。作為分割符,不再大括號內的則作為常量。值得注意的是,和作為2個關鍵字由mvc路由控制,如果確實需要用這個名字命名引數,可以在前面加上 例如。預設的設定路由在global.asax檔案中 public static void registerrou...