ASP.NET Core - 配置



在本章中,我們將討論與 ASP.NET Core 專案相關的配置。在解決方案資源管理器中,您將看到 Startup.cs 檔案。如果您使用過以前版本的 ASP.NET Core,您可能希望看到一個 global.asax 檔案,該檔案是您可以在其中編寫程式碼以在 Web 應用程式啟動期間執行的一個位置。

  • 您可能還會希望看到一個 web.config 檔案,其中包含應用程式執行所需的所有配置引數。

  • 在 ASP.NET Core 中,這些檔案都消失了,取而代之的是從 Startup.cs 載入配置和啟動程式碼。

  • 該檔案中有一個 Startup 類,在這個類中,您可以配置您的應用程式,甚至配置您的配置源。

以下是Startup.cs 檔案中的預設實現。

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

using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging;  

namespace FirstAppDemo { 
   public class Startup { 
      // 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) { 
      }  
      
      // This method gets called by the runtime. Use this method to configure 
      // the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
         ILoggerFactory loggerFactory) { 
         loggerFactory.AddConsole();  
         
         if (env.IsDevelopment()) { 
            app.UseDeveloperExceptionPage(); 
         }  
         app.Run(async (context) => { 
            await context.Response.WriteAsync("Hello World!"); 
         }); 
      } 
   } 
}

在 Startup 類中,有兩個方法是我們大部分工作將發生的地方。類的 Configure 方法是您構建 HTTP 處理管線的地方。

  • 這定義了您的應用程式如何響應請求。目前,此應用程式只能說 Hello World!如果我們希望應用程式表現不同,我們將需要透過在此 Configure 方法中新增其他程式碼來更改管道。

  • 例如,如果我們想提供靜態檔案(例如 index.html 檔案),我們將需要向 Configure 方法新增一些程式碼。

  • 您還可以擁有錯誤頁面或將請求路由到 ASP.NET MVC 控制器;這兩種情況都需要在此 Configure 方法中進行一些工作。

  • 在 Startup 類中,您還將看到ConfigureServices()方法。這有助於您為應用程式配置元件。

現在,我們為每個響應(Hello World!字串)都使用了一個硬編碼字串。與其硬編碼字串,我們希望從知道我們要顯示的文字的某個元件載入此字串。

  • 此其他元件可能會從資料庫或 Web 服務或 JSON 檔案中載入該文字,它在哪裡並不重要。

  • 我們將只設置一個場景,以便我們沒有這個硬編碼字串。

在解決方案資源管理器中,右鍵單擊您的專案節點,然後選擇新增→新建項

Add First Item

在左側窗格中,選擇已安裝→程式碼,然後在中間窗格中,選擇 JSON 檔案。將此檔案命名為AppSettings.json,然後單擊新增按鈕,如上圖所示。

Installed Code

我們還可以讓我們的程式從檔案中讀取文字,而不是在 Startup.cs 中使用 Hello World!字串。讓我們在AppSettings.json 檔案中新增以下程式碼。

{ 
   "message": "Hello, World! this message is from configuration file..." 
}

現在我們需要從 Startup.cs 檔案訪問此訊息。以下是Startup.cs檔案的實現,它將從 JSON 檔案中讀取上述訊息。

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) { 
      }  
      
      // This method gets called by the runtime.  
      // Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app) {
         app.UseIISPlatformHandler();  
         app.Run(async (context) => { 
            var msg = Configuration["message"]; 
            await context.Response.WriteAsync(msg); 
         });  
      }  
        
      // Entry point for the application. 
      public static void Main(string[] args) =7gt; WebApplication.Run<Startup>(args); 
   } 
}

現在讓我們執行應用程式。執行應用程式後,它將生成以下輸出。

Run The Application
廣告

© . All rights reserved.