ASP.NET MVC Routing概覽(C#)

來源:互聯網
上載者:User

在這篇教程裡,將向你介紹每個ASP.NET MVC程式都有的重要特性,叫做 ASP.NET Routing。 ASP.NET Routing 模組負責將傳入的瀏覽器請求映射到特定的MVC控制器actions。教程最後,你會理解標準路由表是如何將請求映射到控制器action的。

使用預設路由表

建立 ASP.NET MVC 程式,它已經配置好使用 ASP.NET Routing了。 ASP.NET Routing 在兩個地方被建立。

第一個, ASP.NET Routing 在程式的Web設定檔(Web.config 檔案)中被啟用。設定檔中有4個與路由相對的節點:system.web.httpModules 節, system.web.httpHandlers 節, system.webserver.modules 節, 和 system.webserver.handlers 節。 小心不要刪除這些節點因為沒有這些節點路由就不能再工作了。

第二個, 也是最重要的一個,路由表在程式的 Global.asax 檔案中被建立。 Global.asax 檔案時一個特殊的檔案,它包括ASP.NET 程式生命週期事件的處理常式。路由表在Application Start 事件之中被建立。

代碼1中包含了ASP.NET MVC程式的預設 Global.asax 檔案。

代碼1 – Global.asax.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace MvcApplication1{    // Note: For instructions on enabling IIS6 or IIS7 classic mode,     // visit http://go.microsoft.com/?LinkId=9394801    public class MvcApplication : System.Web.HttpApplication    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                "Default",                                              // Route name                "{controller}/{action}/{id}",                           // URL with parameters                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults            );        }        protected void Application_Start()        {            RegisterRoutes(RouteTable.Routes);        }    }}

當 MVC 程式初次開機時, Application_Start() 方法被調用。相反,這個方法調用 RegisterRoutes() 方法。 RegisterRoutes() 方法建立路由表。

預設路由表包含一個簡單的路由(名為Default)。預設路由將URL的第一段映射到控制器名稱,第二段映射到控制器action,第三段映射到名為 id 的參數。

想象你輸入以下URL到你的瀏覽器地址欄中:

/Home/Index/3

預設路由將此URL映射到以下參數:

    controller = Home

    action = Index

    id = 3

當你請求此 URL /Home/Index/3, 以下代碼將被執行:

HomeController.Index(3)

預設路由度這三個參數都包括了預設值。如果你不提供controller,那麼controller參數預設為 Home。 如果你不提供action,那麼action預設為 Index。 最後,如果你不提供id, id參數預設為空白字串。

讓我們來看看一些預設路由如何映射URL到控制器action的例子。想象你輸入以下URL到你的瀏覽器地址欄中:

/Home

由於預設路由的預設值,這個 URL 會調用 HomeController類的 Index() ,如代碼2所示。

代碼2 – HomeController.cs

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(string id)        {            return View();        }    }}

在代碼2中,HomeController 類包括一個名為 Index() 的方法,它接受一個名為Id的簡單參數。 URL /Home 使得 Index() 方法在Id參數的值為空白字串的情況下被調用。

由於MVC架構調用控制器actions的方式,URL /Home 也符合代碼3中HomeController類的 Index() 方法。

代碼3 – HomeController.cs (無參Index action)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index()        {            return View();        }    }}

代碼3中的 Index() 方法不接受任何參數。 URL /Home 會使得 Index() 方法被調用。 URL /Home/Index/3 也會調用這個方法(Id 被忽略)。

URL /Home 還符合代碼4中HomeController類的 Index() 方法。

代碼 4 – HomeController.cs (帶有可空型別參數的Index action)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(int? id)        {            return View();        }    }}

 

在代碼4中, Index() 方法具有一個整形參數。由於該參數為可空參數(值可以為null),Index() 方法可以被調用而不產生任何錯誤。

最後,用URL /Home來調用代碼5中的 Index() 方法會導致異常,因為參數不是一個可空參數。如果你試圖調用Index()方法那麼就會得到圖1所示的錯誤。

代碼5 – HomeController.cs (帶有Id參數的Index action)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(int id)        {            return View();        }    }}

圖01: 調用帶參數的控制器 action (點擊查看完整大小)

另一方面,URL /Home/Index/3 在代碼5中的Index控制器中就會運行良好。請求 /Home/Index/3 使得 Index() 方法帶上值為3的Id參數被調用。

總結

這篇教程的母的是要向你簡單介紹 ASP.NET Routing。我們考查了由ASP.NET MVC程式得到的預設路由表。你學到了預設路由如何將URL映射到控制actions。

原文地址:http://www.asp.net/learn/mvc/tutorial-05-cs.aspx

聯繫我們

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