ASP.NET Core 2.2 series [a] project to build ASP.NET Core WebApi

A step

Select "New"> "Project" from the "File" menu.

Select "ASP.NET Core Web Application" template, and then click "Next."

Name the project NetCoreWebApi, and then click "Create."

Select ".NET Core" and "ASP.NET Core 2.2". Select "API" template, and then click "Create."

Once created, the project is structured as follows:

Second, the project interpretation

Properties——launchSettings.json

Startup configuration file, save a ASP.NET Core application-specific standard configuration, start the preparatory work for the application, including environment variables, the development of ports.

1  {
 2    " $ Schema " : " http://json.schemastore.org/launchsettings.json " ,
 3    " iisSettings " : { // choose to start IIS Express 
4      " windowsAuthentication " : false , // whether to enable windows identity verify 
5      " anonymousAuthentication " : to true , // whether to enable anonymous authentication 
6      " iisexpress " : {
 7        " ApplicationURL " :"HTTP: // localhost: 60 953 " , // the IIS Express random port 
. 8        " SSLPort " : 0 
. 9      }
 10    },
 . 11    " Profiles " : {
 12 is      " the IIS Express " : {
 13 is        " commandName " : " iisexpress " ,
 14        " launchBrowser " : to true , // whether to activate the browser 
15        " launchUrl " : "api/values" , // browser starts opposite the URL 
16        " EnvironmentVariables " : { // set the environment variable key / value pairs 
. 17          " ASPNETCORE_ENVIRONMENT " : " Development " 
18 is        }
 . 19      },
 20 is      " NetCoreWebApi " : {
 21 is        " commandName " : " Project " ,
 22        " launchBrowser " : to true ,
 23        " launchUrl ": "api/values",
24       "applicationUrl": "http://localhost:5000",
25       "environmentVariables": {
26         "ASPNETCORE_ENVIRONMENT": "Development"
27       }
28     }
29   }
30 }

appsetting.json

appsetting.json is the application configuration file, similar to the Web.config configuration file ASP.NET MVC application.

For example: the connection string, and so on ....

 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 file entry application is carried out by starting UseStartup <Startup> () Startup boot file specified below after starting.

. 1   public  class Program
 2      {
 . 3          public  static  void the Main ( String [] args)
 . 4          {
 . 5              CreateWebHostBuilder (args) .build () the Run ();.
 . 6          }
 . 7  
. 8          public  static IWebHostBuilder CreateWebHostBuilder ( String [] args) =>
 . 9              WebHost.CreateDefaultBuilder (args) // create WebHostBuilder. 
10                  .UseStartup <the Startup> (); // specified start classes for injection and middleware dependency registration. 
11      }

Startup.cs

This file is the default file, not arbitrarily delete, in this document may include service configuration, important operational definitions request processing pipeline.

ConfigureServices service method for serving IServiceCollection poured into a container.

Configure a method for application in response to an HTTP request, to the intermediate register to the configuration request ApplicationBuilder pipe.

 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          // response to HTTP requests. Middleware can register the instance configuration request IApplicationBuilder pipe. 
. 17          public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env)
 18 is          {
 . 19              IF (env.IsDevelopment ())
 20 is              {
 21 is                  app.UseDeveloperExceptionPage ();
 22 is              }
 23 is  
24              app.UseMvc ();
 25          }
 26 is      }

Third, the test

At this point the project has been completed, then press Ctrl + F5 to run, you can see 

This is one of the most basic environment of .NET Core API, API framework we want from a long way, but is already half done, because a good start is half the battle!

 

Guess you like

Origin www.cnblogs.com/tenghao510/p/11911078.html