asp.net core 教程(五)-配置


Asp.Net Core-配置

在這一章,我們將討論 ASP.NET Core項目的相關的配置。在解決方案資源管理器中,您將看到 Startup.cs 文件。如果你有以前版本的 ASP.NET的工作經驗,你可能希望看到一個 global.asax 文件,您可以在其中編寫代碼,它是一個編寫程序啟動時立即執行的代碼的文件。

  • 你可能也希望看到一個 web.config 文件,該文件包含您的應用程序執行所需的所有配置參數。

  • 在 ASP.NET Core中,那些文件都沒了,取而代之的是 Startup.cs文件.

  • Startup.cs里面是一個啟動類文件,並在該類中您可以配置您的應用程序甚至配置您的配置資源。

這里是 Startup.cs 文件中的默認實現代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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, 
       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!" ); 
          }); 
      
   
}

 

在啟動類中,我們的大部分工作將設計有兩種方法。Configure 方法是構建HTTP處理管道的地方。

  • 這定義了應用程序如何響應請求。目前該應用程序只能說“Hello World!”如果我們希望該應用程序具有不同的行為,我們需要通過添加額外的代碼到這個Configure方法中來改變周圍的管道。

  • 例如,如果我們想要提供一個 index.html 文件的靜態文件,我們將需要在Configure方法中添加一些代碼。

  • 你也可以有一個錯誤頁面或Asp.Net Controller的異常請求的路由;這兩個場景還需要在這個配置方法中做一些工作。

  • 在啟動類中,您還將看到 ConfigureServices() 方法。這可幫助您配置您的應用程序的組件。

現在,我們有一個硬編碼的字符串“Hello World !”來響應每個請求。我們不希望每個請求都是硬編碼的字符串,我們想從一些組件加載響應字符串。

  • 其他組件可能會從數據庫加載文本,或從一個web服務或一個JSON文件,我們不管這它是從什么地方加載。

  • 我們會設置一個場景,這樣我們就沒有這個硬編碼字符串了。

在解決方案資源管理器中,右鍵單擊您的項目節點並選擇Add→New Item。

在左側窗格中,選擇Installed → Code,然后在中間窗格中,選擇JSON文件。給這個文件取名為AppSetting.json,並單擊Add按鈕如上面的截圖。

讓我們在AppSettings中添加以下代碼。

1
2
3
    "message" "Hello, World! this message is from configuration file..." 
}

現在我們需要從 Startup.cs 文件訪問此消息。這里是 Startup.cs 文件從 JSON 文件閱讀上面的消息的實現代碼。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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, 
       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); 
   
}

讓我們現在運行應用程序。一旦您運行該應用程序,它會產生下面的輸出。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM