ASP.NET MVC - 路由



路由是將 HTTP 請求定向到控制器的過程,此處理功能在 System.Web.Routing 中實現。此程式集不是 ASP.NET MVC 的一部分。它實際上是 ASP.NET 執行時的一部分,並隨 ASP.NET .NET 3.5 SP1 正式釋出。

MVC 框架使用System.Web.Routing,但 ASP.NET 動態資料也使用它。MVC 框架利用路由將請求定向到控制器。Global.asax 檔案是應用程式的一部分,您將在其中定義應用程式的路由。

這是我們上一章中建立的 MVC 應用程式中 Global.asax 中應用程式啟動事件的程式碼。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCFirstApp {
   public class MvcApplication : System.Web.HttpApplication {
      protected void Application_Start(){
         AreaRegistration.RegisterAllAreas();
         RouteConfig.RegisterRoutes(RouteTable.Routes);
      }
   }
}

以下是 RouteConfig 類的實現,其中包含一個方法 RegisterRoutes。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCFirstApp {
   public class 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});
      }
   }
}

您將定義路由,這些路由將 URL 對映到特定的控制器操作。操作只是控制器上的一個方法。它還可以從該 URL 中提取引數並將它們作為引數傳遞到方法中。

因此,在應用程式中定義的此路由是預設路由。如上述程式碼所示,當您看到以 (something)/(something)/(something) 形式到達的 URL 時,第一部分是控制器名稱,第二部分是操作名稱,第三部分是 ID 引數。

理解路由

MVC 應用程式使用 ASP.NET 路由系統,該系統決定 URL 如何對映到控制器和操作。

當 Visual Studio 建立 MVC 專案時,它會新增一些預設路由以幫助我們入門。執行應用程式時,您會看到 Visual Studio 已將瀏覽器定向到埠 63664。您幾乎肯定會看到瀏覽器請求的 URL 中不同的埠號,因為 Visual Studio 在建立專案時會分配隨機埠。

Localhost Home

在最後一個示例中,我們添加了一個 HomeController,因此您也可以請求以下任何 URL,它們將被定向到 HomeController 上的 Index 操作。

https://:63664/Home/

https://:63664/Home/Index

當瀏覽器請求 http://mysite/ 或 http://mysite/Home 時,它會返回 HomeController 的 Index 方法的輸出。

您也可以嘗試透過更改瀏覽器中的 URL 來實現此目的。在此示例中,它是 https://:63664/,只是埠可能不同。

如果將 /Home 或 /Home/Index 附加到 URL 並按“Enter”按鈕,您將看到 MVC 應用程式的相同結果。

Localhost Home Index

正如您在此例中看到的,約定是我們有一個名為 HomeController 的控制器,此 HomeController 將成為我們 MVC 應用程式的起點。

Visual Studio 為新專案建立的預設路由假定您將遵循此約定。但是,如果您想遵循自己的約定,則需要修改路由。

自定義約定

您當然可以新增自己的路由。如果您不喜歡這些操作名稱,如果您有不同的 ID 引數,或者如果您通常對站點的 URL 結構不同,則可以新增自己的路由條目。

讓我們來看一個簡單的例子。假設我們有一個包含程序列表的頁面。以下是將路由到該程序頁面的程式碼。

routes.MapRoute(
   "Process",
   "Process/{action}/{id}",
   defaults: new{
      controller = "Process", action = "List ", id = UrlParameter.Optional}
);

當有人進入並查詢帶有 Process/Action/Id 的 URL 時,他們將轉到 Process 控制器。我們可以使操作略有不同,預設操作,我們可以將其設為 List 而不是 Index。

現在到達的請求看起來像 localhosts/process。路由引擎將使用此路由配置將其傳遞,因此它將使用 List 的預設操作。

以下是完整的類實現。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCFirstApp{
   public class RouteConfig{
      public static void RegisterRoutes(RouteCollection routes){
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
			
         routes.MapRoute(
            "Process", "Process/{action}/{id}",
            defaults: new{
               controller = " Process", action = "List ", id =
               UrlParameter.Optional});
					
         routes.MapRoute(
            name: "Default", url: "{controller}/{action}/{id}",
            defaults: new{
               controller = "Home", action = "Index", id =
               UrlParameter.Optional});
      }
   }
}

步驟 1 - 執行此程式並使用以下 URL 請求程序頁面 https://:63664/Process

Localhost Process

您將看到 HTTP 404,因為路由引擎正在查詢 ProcessController,而該控制器不可用。

步驟 2 - 透過右鍵單擊解決方案資源管理器中的 Controllers 資料夾並選擇新增 → 控制器來建立 ProcessController。

Create ProcessController

它將顯示“新增腳手架”對話方塊。

ProcessController Scaffolding Dialog

步驟 3 - 選擇 MVC 5 控制器 - 空選項,然後單擊“新增”按鈕。

將出現“新增控制器”對話方塊。

Empty Option Add Button

步驟 4 - 將名稱設定為 ProcessController 並單擊“新增”按鈕。

現在,您將在 Controllers 資料夾中看到一個新的 C# 檔案 ProcessController.cs,該檔案也將在 Visual Studio 中開啟以進行編輯。

Set ProcessController

現在我們的預設操作將是 List,因此我們希望在此處使用 List 操作而不是 Index。

步驟 5 - 將返回型別從 ActionResult 更改為 string,並使用以下程式碼從此操作方法返回一些字串。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCFirstApp.Controllers{
   public class ProcessController : Controller{
      // GET: Process
      public string List(){
         return "This is Process page";
      }
   }
}

步驟 6 - 當您執行此應用程式時,您將再次看到預設路由的結果。當您指定以下 URL 時,https://:63664/Process/List,然後您將看到 ProcessController 的結果。

Localhost Process List
廣告