Asp.Net MVC2.0 Url 路由入門---執行個體篇

來源:互聯網
上載者:User

標籤:

本篇主要講述Routing組件的作用,以及舉幾個執行個體來學習Asp.Net MVC2.0 Url路由技術。

     接著上一篇開始講,我們在Global.asax中註冊一條路由後,我們的請求是怎麼轉到相應的View的呢?Controller和Action是怎麼解析的?這就是Routing組件乾的事情了。

     Routing的作用:它首先是擷取到View傳過來的請求,並解析Url請求中Controller和Action以及資料,其次他將識別出來的資料傳遞給Controller的Action(Controller的方法)。這是Routing組件的兩個重要的作用!

下面我們從幾個例子來講解一下Url路由的使用。

MapRoute()有6個方法可以重載,下面舉5個例子相應介紹!

執行個體一:首先講解的是系統預設提供的路由格式,下面是系統給的預設代碼:

?
12345678910 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 } // 參數預設值    );  }

Url格式為:http://localhost:0000/home/index  對應規則為:{controller}/{action}/{id}  黑體部分就是對應部分。這還是有預設值的情況。

詳細匹配應該為:http://localhost:0000/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("沒有預設值路由規則", "{controller}/{id}-{action}");

  適合的Url例子:http://localhost:0000/Custom/1-Detials  

  它將不匹配http://localhost:0000/

執行個體三:帶名稱空間的Url路由規則

      函數頭:MapRoute( string name, string url, string[] namespaces);//路由名,Url規則,名稱空間

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

       Url:http://localhost:0000/Custom/1-Detials

       這個例子是帶命名空間的路由規則,這在Aeras使用時非常有用。不多說,後面再說!

執行個體四:帶約束的路由規則

      函數頭:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url規則,預設值,名稱空間

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

       Url:http://localhost:14039/home/index-2010-01-21

執行個體五:帶名稱空間,帶約束,帶預設值的路由規則

      函數頭:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

        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[] { "MvcDemo.Controllers" }             );

        Url:http://localhost:14039/Admin/home/index-2010-01-21

執行個體六:捕獲所有的路由

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

關於Global.asax剩餘部分的說明:

       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");是忽略這個規則的Url

 

   AreaRegistration.RegisterAllAreas();//註冊所有的Areas        RegisterRoutes(RouteTable.Routes);//註冊我們寫的規則        //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);//調試用語句,需要下載RouteDebug.dll,並添加引用!加入這句話後就可以測試Url路由了。

 

 

初識Asp.Net MVC2.0初識Asp.Net MVC2.0【續】

Asp.Net MVC2.0 Url 路由入門---執行個體篇

Asp.Net MVC2.0 Url 路由入門

Asp.Net MVC3 簡單入門第一季(一)環境準備

Asp.Net MVC3 簡單入門第一季(二)詳解Asp.Net MVC3項目

Asp.Net MVC3 簡單入門第一季(三)詳解Controller之Filter

Asp.Net MVC3 簡單入門第一季(四)詳解Request Processing Pipeline

 

轉載:http://www.cnblogs.com/fly_dragon/archive/2010/10/09/1846556.html

Asp.Net MVC2.0 Url 路由入門---執行個體篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.