ASP.NET Core 2.2 系列【一】搭建ASP.NET Core WebApi項目

1、步驟

從「文件」菜單中選擇「新建」>「項目」 。json

選擇「ASP.NET Core Web 應用程序」模板,再單擊「下一步」 。windows

將項目命名爲 NetCoreWebApi,而後單擊「建立」 。api

選擇「.NET Core」和「ASP.NET Core 2.2」 。 選擇「API」模板,而後單擊「建立」 。瀏覽器

建立完成後,項目結構以下:app

2、項目解讀

Properties——launchSettings.json

啓動配置文件,一個ASP.NET Core應用保存特有的配置標準,用於應用的啓動準備工做,包括環境變量,開發端口等。框架

 1 {
 2   "$schema": "http://json.schemastore.org/launchsettings.json",
 3   "iisSettings": { //選擇以IIS Express啓動 
 4     "windowsAuthentication": false, //是否啓用windows身份驗證
 5     "anonymousAuthentication": true, //是否啓用匿名身份驗證
 6     "iisExpress": {
 7       "applicationUrl": "http://localhost:60953", //IIS Express隨機端口
 8       "sslPort": 0
 9     }
10   },
11   "profiles": {
12     "IIS Express": {
13       "commandName": "IISExpress",
14       "launchBrowser": true, //是否在瀏覽器中啓動
15       "launchUrl": "api/values", //在瀏覽器中啓動的相對URL
16       "environmentVariables": { //將環境變量設置爲鍵/值對
17         "ASPNETCORE_ENVIRONMENT": "Development"
18       }
19     },
20     "NetCoreWebApi": {
21       "commandName": "Project",
22       "launchBrowser": true,
23       "launchUrl": "api/values",
24       "applicationUrl": "http://localhost:5000",
25       "environmentVariables": {
26         "ASPNETCORE_ENVIRONMENT": "Development"
27       }
28     }
29   }
30 }

appsetting.json

appsetting.json是應用程序配置文件,相似於ASP.NET MVC應用程序中的Web.config配置文件。測試

例如:鏈接字符串,等等....ui

 1 {
 2     "Logging":{
 3         "LogLevel":{
 4             "Default":"Warning"
 5         }
 6     },
 7     "AllowedHosts":"*",
 8     "ConnectionStrings":{
 9         "SqlServer":"Data Source=.;Initial Catalog=NetCoreWebApi;User Id=sa;Password=123;"
10     }
11 }

Program.cs

Program.cs文件是應用的入口, 啓動後經過UseStartup<Startup>()指定下文的Startup啓動文件進行啓動。spa

 1  public class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             CreateWebHostBuilder(args).Build().Run();
 6         }
 7 
 8         public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
 9             WebHost.CreateDefaultBuilder(args)//建立WebHostBuilder。
10                 .UseStartup<Startup>();//指定啓動類,用於依賴注入和中間件註冊。
11     }

Startup.cs

該文件是默認文件,不可隨意刪除,在此文件中能夠以包含服務配置、定義請求處理管道的重要操做。code

ConfigureServices 方法用於將服務注入到 IServiceCollection 服務容器中。

Configure方法用於應用響應 HTTP 請求,將中間件註冊到 ApplicationBuilder中來配置請求管道。

 1  public class Startup
 2     {
 3         public Startup(IConfiguration configuration)
 4         {
 5             Configuration = configuration;
 6         }
 7 
 8         public IConfiguration Configuration { get; }
 9 
10         //負責注入服務
11         public void ConfigureServices(IServiceCollection services)
12         {
13             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
14         }
15 
16         // 響應 HTTP 請求的方式。可將中間件註冊到IApplicationBuilder 實例來配置請求管道。
17         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
18         {
19             if (env.IsDevelopment())
20             {
21                 app.UseDeveloperExceptionPage();
22             }
23 
24             app.UseMvc();
25         }
26     }

3、測試

此時項目已經完成,按 Ctrl+F5 運行以後,就能看到 

這是一個最基礎的.NET Core API環境,離咱們想要的API框架還很遠,可是其實已經成功一半了,由於好的開始是成功的一半!

相關文章
相關標籤/搜索