ASP.NET MVC 學習一之路由

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   sp   strong   檔案   

  本次學習的所有開發環境皆為VS2012+MVC4 一:什麼是MVC:

     假設你是第一次學習MVC,好吧我們不去理解它內部是如何去實現功能的,我們只從它表面的字母意思先去入門理解吧

     M:models(模型)  V:view(視圖)  C:controllers(控制器)

    先不要去糾結這三個字母是什麼東西吧,以及這三個東西有什麼用!我們所要用的就是先記下這個三個字母!呵呵,是不是有點死記硬背的趕腳,我也是這麼覺得的,有點想扔磚的感覺!(麻痹,博主想講的啥!!!-__-!)

二:MVC學習之路由

在此建立一個測試專案Home,並添加一個Controllers取名為HomeControllers,並為其添加一個Index視圖;

專案檔:

雙擊開啟Index.cshtml視圖,更改其代碼:

@{    ViewBag.Title = "Index";}<h2>Hello World</h2>

運行項目我們已經發現程式能正確運行

 

也許到這你可能會驚訝於mvc是如何能工作並找到其工作檔案的.

呵呵,聰明的你可能已經猜到了接下來就是路由登場了。

雙擊RouteConfig檔案

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

這就是MVC內部定義的路由機制

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

對著句代碼的意思先暫且理解為
對所有axd的資源 進行忽略,直接進行URL訪問
routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );

這是一條簡單的路由規則,name屬性定義了路由的名稱,url定義了路由的映射機制,defaults定義了路由的預設參數

實際{controller}就是對應著我們前面的controller的名稱Home,action對應我們前面定義的Index,所以我們在瀏覽器中輸入

www.Test.com/Home/Index就可以成功看到效果了!

 

好吧,到這裡我們來定義一個屬於自己的路由吧

routes.MapRoute(                name: "myRoute",                url: "{controller}-{action}"                );
這裡我沒有給myRoute的路由定義預設的屬性,當然我們運行項目,項目初始化後當然是找不到的,手動在瀏覽器中輸入

www.Test.com/Home-Index就可以正確顯示視圖了,到這你是不是能體會到mvc的一個優勢來呢,當然你可以定義更多個人化的url路由來方便SEO

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

這裡需要補充一下,在這個項目中如果定義了兩個路由規則,mvc會自動從上往下匹配路由規則,如果myRoute的路由規則沒有匹配上就會匹配default的路由規則,如果兩個都沒匹配到,則會報錯!

路由值的簡單約束

剛才在name為“Default”的路由上id = UrlParameter.Optional,這句話的意思是id為可選輸入,當我們去掉id = UrlParameter.Optional的時候,運行項目發現報錯了,在url上給地址加上www.Test.com/Home/Index/1路由正確。

 

routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },                //加上路由值的條件約束                constraints: new                {                    id = @"\d+"                }            );

此時我們給路由器的id屬性添加一個約束條件規定id只能為數字,如果此時在瀏覽器輸入

www.Test.com/Home/Index/1test的url則會報錯

 

附上一個自己總結的幾個路由規則吧

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },                constraints: new { controller = @"\d{4}" }               // constraints: new { controller [email protected]"\d{4}"}                );                  //hotels/所有匹配            routes.MapRoute(                name: "酒店首頁",                url: "Home/{*values}",                defaults: new { controller = "Home", action = "Index", hotelid = "" }            );            routes.MapRoute(                name: "網站首頁",                url: "{*values}",                defaults: new { controller = "Home", action = "Index" }                );

ASP.NET MVC 學習一之路由

聯繫我們

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