【ASP.NET專題】(3)——ASP.NET MVC中的URL Routing

來源:互聯網
上載者:User
文章目錄
  • 一、什麼是URL Routing?
  • 二、基本URL Routing規則的定義
  • 三、URL Routing解決的問題
  • 四、URL Routing相關的資料

《【ASP.NET專題】(1)——ASP.NET MVC初探》:http://blog.csdn.net/rocket5725/archive/2010/01/11/5177320.aspx

留下了一個問題:我們如何想看到Index.aspx和About.aspx的頁面我們應該怎麼辦呢?

答案就是我們可以使用URL Routing來配置一些URL的映射,使使用者可以按你的規則來訪問網站。

一、什麼是URL Routing?

使用URL Routing,一定要規定URL模式,它包括一個位置標識,它將在你請求網頁時按這個規則返回給你內容. 當然,這個建立的規則完全是由你自己定義的.
上回說道如何訪問index.aspx及about.aspx?
但是http://localhost/Views/Home/Index.aspx和 http://localhost/Views/Home/About.aspx這兩個地址並無法直接存取以下兩個檔案:Views/Home/Index.aspx 與 Views/Home/About.aspx
這是怎麼回事呢,那我們要怎樣才能訪問呢 ?
答案是: http://localhost/Home和http://localhost/Home/About
這是為什麼呢?
這就是UrlRouting的功能,而這個功能的配置是由,URL的請求規則定義的,這個規則定義在Global.asax.cs中定義。

二、基本URL Routing規則的定義

我們可以查看到Global.asax.cs的代碼,如下:

using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Mvc;<br />using System.Web.Routing;</p><p>namespace MvcApplication2<br />{<br /> /// <summary><br /> /// 還是老規矩,按序號看<br /> /// </summary><br /> // Note: For instructions on enabling IIS6 or IIS7 classic mode,<br /> // visit http://go.microsoft.com/?LinkId=9394801<br /> public class MvcApplication : System.Web.HttpApplication<br /> {<br /> // 4.注意: 將1標的規則更改為 "{controller}.mvc/{action}/{id}" 即可<br /> // 自行支援 IIS6 and IIS7 兩種模式<br /> // 筆者注:一般的虛擬機器主機不支援.mvc,.aspx也要檢查檔案存在<br /> // 變通方法為可以將.mvc換成.ashx或.asbx</p><p> public static void RegisterRoutes(RouteCollection routes)<br /> {<br /> //5、此路徑不受後面規則控制<br /> routes.IgnoreRoute("{resource}.axd/{*pathInfo}");<br /> //1、大家可以回憶一下Controller和Action的定義<br /> //因為MVC與傳統Aspx的最大不同就是訪問是<br /> //訪問的Controller.Action即某類下的一個函數而不是aspx檔案,<br /> //要展現給使用者哪一個aspx檔案是由Controller決定的(即預設的同名規則)<br /> //這個是檔案預設內建的UrlRouting規則,是將Controller/Action/id的訪問<br /> //模式指向對應的Controller及Action<br /> routes.MapRoute(<br /> "Default", // Route 名稱<br /> "{controller}/{action}/{id}", // URL參數<br /> new { controller = "Home", action = "Index", id = "" }<br /> // 2、參數的預設值也就是如果各部分為空白的話訪問<br /> //HomeController下的Index這個Action<br /> );<br /> }<br /> protected void Application_Start()<br /> { //3.這個沒什麼好講了,就是在應用程式啟動時初始化它<br /> RegisterRoutes(RouteTable.Routes);<br /> }<br /> }<br />}

注意這一點ASP.NET MVC 中URLRouting只與Controller/Action有關。

三、URL Routing解決的問題

UrlRouting是為了讓Url更簡短更有意義才出現的,例如:post.aspx?year=1999&month=3&day=8 的參數URL變為
/post/1999/3/8/ 這樣的簡短漂亮且有意義的URL

而類似home/about.aspx?id=12,以樣本中的Global.asax.cs中定義的{controller}/{action}/{id}規則為列/Home/About/12其實就是訪問Controller="Home" Action="About" 它的一個QueryString參數為 id="12"

四、URL Routing相關的資料

為尊重原創,現給出地址:

System.Web.Routing入門及進階 上篇

System.Web.Routing入門及進階 下篇

System.Web.Routing 的說明文檔

聯繫我們

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