ASP.NET Core - 設定 MVC



在本章中,我們將設定 FirstAppDemo 應用程式中的 MVC 框架。我們將透過構建一個基於 ASP.NET Core 的 Web 應用程式,更具體地說,是基於 ASP.NET Core MVC 框架的 Web 應用程式來進行。從技術上講,我們只使用中介軟體就可以構建整個應用程式,但是 ASP.NET Core MVC 為我們提供了可以輕鬆建立 HTML 頁面和基於 HTTP 的 API 的功能。

要在我們的空專案中設定 MVC 框架,請按照以下步驟操作:

  • 安裝 **Microsoft.AspNet.Mvc** 包,它使我們能夠訪問框架提供的程式集和類。

  • 安裝完程式包後,我們需要在執行時註冊 ASP.NET MVC 所需的所有服務。我們將在 **ConfigureServices** 方法中執行此操作。

  • 最後,我們需要新增 ASP.NET MVC 的中介軟體來接收請求。從本質上講,這部分中介軟體接收 HTTP 請求並嘗試將該請求定向到我們將編寫的 C# 類。

**步驟 1** - 讓我們透過右鍵單擊“管理 NuGet 程式包”來進入 NuGet 程式包管理器。安裝 Microsoft.AspNet.Mvc 包,它使我們能夠訪問框架提供的程式集和類。

Microsoft.AspNet.MVC

**步驟 2** - 安裝 Microsoft.AspNet.Mvc 包後,我們需要在執行時註冊 ASP.NET Core MVC 所需的所有服務。我們將使用 ConfigureServices 方法來執行此操作。我們還將新增一個簡單的控制器,並檢視該控制器的一些輸出。

讓我們為此專案新增一個新資料夾,並將其命名為 **Controllers**。我們可以在此資料夾中放置多個控制器,如下面的解決方案資源管理器所示。

Controllers

現在右鍵單擊 Controllers 資料夾,然後選擇 **新增 → 類** 選單選項。

Add Class

**步驟 3** - 在這裡,我們想要新增一個簡單的 **C#** 類,並將此類命名為 **HomeController**,然後單擊“新增”按鈕,如上面的螢幕截圖所示。

Home Controller

這將是我們的預設頁面。

**步驟 4** - 讓我們定義一個返回字串的公共方法,並將該方法命名為 Index,如以下程式所示。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks;  

namespace FirstAppdemo.Controllers { 
   public class HomeController { 
      public string Index() { 
         return "Hello, World! this message is from Home Controller..."; 
      } 
   } 
}

**步驟 5** - 當您轉到網站的根目錄時,您希望看到控制器響應。目前,我們將提供 index.html 檔案。

Controller Response

讓我們進入網站的根目錄並刪除 index.html。我們希望控制器響應而不是 **index.html** 檔案。

**步驟 6** - 現在轉到 Startup 類中的 Configure 方法,並新增 **UseMvcWithDefaultRoute** 中介軟體。

UseMvc Default Route

**步驟 7** - 現在重新整理網站根目錄處的應用程式。

Refresh the Application

您將遇到 500 錯誤。錯誤指出框架無法找到所需的 ASP.NET Core MVC 服務。

ASP.NET Core 框架本身由具有非常集中職責的不同小型元件組成。

例如,有一個元件必須定位並例項化控制器。

該元件需要位於 ASP.NET Core MVC 正確執行的服務集合中。

**步驟 8** - 除了新增 NuGet 包和中介軟體外,我們還需要在 ConfigureServices 中新增 AddMvc 服務。以下是 Startup 類的完整實現。

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Hosting; 
using Microsoft.AspNet.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Configuration;  

namespace FirstAppDemo { 
   public class Startup { 
      public Startup() { 
         var builder = new ConfigurationBuilder() .AddJsonFile("AppSettings.json"); 
         Configuration = builder.Build(); 
      }  
      public IConfiguration Configuration { get; set; }
      
      // This method gets called by the runtime. 
      // Use this method to add services to the container. 
      // For more information on how to configure your application, 
      // visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
         services.AddMvc(); 
      }
      
      // This method gets called by the runtime.  
      // Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app) { 
         app.UseIISPlatformHandler();  
         
         app.UseDeveloperExceptionPage(); 
         app.UseRuntimeInfoPage();  
         
         app.UseFileServer(); 
         app.UseMvcWithDefaultRoute();  
         
         app.Run(async (context) => { 
            var msg = Configuration["message"]; 
            await context.Response.WriteAsync(msg); 
         });
      } 
      
      // Entry point for the application. 
      public static void Main(string[] args) => WebApplication.Run<Startup>(args); 
   }  
}            

**步驟 9** - 儲存 **Startup.cs** 檔案,然後轉到瀏覽器並重新整理它。您現在將收到來自我們的 **home 控制器** 的響應。

Startup.Cs HomeController
廣告

© . All rights reserved.