ASP.NET MVC - 動作



ASP.NET MVC 動作方法負責執行請求並生成響應。預設情況下,它以 ActionResult 的形式生成響應。動作通常與使用者互動具有一對一對映關係。

例如,在瀏覽器中輸入 URL,點選任何特定連結以及提交表單等。每個使用者互動都會導致向伺服器傳送請求。在每種情況下,請求的 URL 都包含 MVC 框架用於呼叫動作方法的資訊。動作方法的唯一限制是它們必須是例項方法,因此不能是靜態方法。此外,沒有返回值限制。因此,您可以返回字串、整數等。

請求處理

動作是 MVC 應用程式中最終的請求目標,它使用控制器基類。讓我們來看一下請求處理。

  • 當一個 URL 到達時,例如 /Home/index,UrlRoutingModule 會檢查並理解路由表中配置的內容知道如何處理該 URL。

Request Processing
  • UrlRoutingModule 將我們在路由表中配置的資訊組合在一起,並將控制權交給 MVC 路由處理程式。

  • MVC 路由處理程式將控制器交給 MvcHandler,它是一個 HTTP 處理程式。

  • MvcHandler 使用控制器工廠來例項化控制器,它知道要例項化哪個控制器,因為它在 RouteData 中查詢該控制器值。

  • 一旦 MvcHandler 有了一個控制器,MvcHandler 只知道 IController 介面,因此它只是告訴控制器執行。

  • 當它告訴控制器執行時,它已經從 MVC 的控制器基類派生。Execute 方法建立一個動作呼叫器,並告訴該動作呼叫器去查詢要呼叫的方法,查詢要呼叫的動作。

  • 動作呼叫器再次檢視 RouteData 並找到從路由引擎傳遞過來的 action 引數。

動作型別

動作基本上返回不同型別的操作結果。ActionResult 類是所有操作結果的基類。以下是不同型別的操作結果及其行為的列表。

序號 名稱和行為
1

ContentResult

返回一個字串

2

FileContentResult

返回檔案內容

3

FilePathResult

返回檔案內容

4

FileStreamResult

返回檔案內容

5

EmptyResult

不返回任何內容

6

JavaScriptResult

返回要執行的指令碼

7

JsonResult

返回 JSON 格式的資料

8

RedirectToResult

重定向到指定的 URL

9

HttpUnauthorizedResult

返回 403 HTTP 狀態程式碼

10

RedirectToRouteResult

重定向到不同的動作/不同的控制器動作

11

ViewResult

作為檢視引擎的響應接收

12

PartialViewResult

作為檢視引擎的響應接收

讓我們來看一下上一章中我們建立的 EmployeeController 的一個簡單示例。

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

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

namespace MVCControllerDemo.Controllers {
   public class EmployeeController : Controller{
      // GET: Employee
      public ActionResult Search(string name){
         var input = Server.HtmlEncode(name);
         return Content(input);
      }
   }
}

當您請求以下 URL **https://:61465/Employee/Mark** 時,您將收到以下輸出作為動作。

Localhost Employee Mark Output

新增控制器

讓我們再新增一個控制器。

**步驟 1** - 右鍵單擊 Controllers 資料夾,然後選擇新增 -> 控制器。

Add Another Controller

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

Add Scaffolding Dialog

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

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

CustomerController

**步驟 3** - 將名稱設定為 CustomerController 並單擊“新增”按鈕。

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

set_name CustomerController

同樣,新增另一個名為 HomeController 的控制器。以下是 HomeController.cs 類的實現。

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

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

namespace MVCControllerDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public string Index(){
         return "This is Home Controller";
      }
   }
}

**步驟 4** - 執行此應用程式,您將收到以下輸出。

Home Controller Output

**步驟 5** - 在我們上面建立的 Customer 控制器中新增以下程式碼。

public string GetAllCustomers(){
   return @"<ul>
      <li>Ali Raza</li>
      <li>Mark Upston</li>
      <li>Allan Bommer</li>
      <li>Greg Jerry</li>
   </ul>";
}

**步驟 6** - 執行此應用程式並請求 **https://:61465/Customer/GetAllCustomers**。您將看到以下輸出。

Localhost GetAllCustomers

您還可以重定向到同一控制器的操作,甚至重定向到不同控制器的操作。

以下是一個簡單的示例,我們將使用以下程式碼更改 HomeController 中的程式碼,從 HomeController 重定向到 Customer Controller。

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

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

namespace MVCControllerDemo.Controllers{
   public class HomeController : Controller{
      // GET: Home
      public ActionResult Index(){
         return RedirectToAction("GetAllCustomers","Customer");
      }
   }
}

如您所見,我們使用了 RedirectToAction() 方法 ActionResult,它有兩個引數,操作名稱和控制器名稱。

執行此應用程式後,您將看到預設路由會將其重定向到 /Customer/GetAllCustomers

Localhost Customers GetAllCustomers
廣告