ASP.NET Core 3.0 WebApi series [1] project to create ASP.NET Core WebApi

EDITORIAL


You can create C # language RESTfulservice, it is called WebApi. Summarized here support the use of learning to create, read, update, delete (CRUD) operations to create ASP.NET Core RESTfulServices.

First, the operating environment


[1] Operating environment: win10 Home Edition
[2] development tools: Visual Studio 2019 16.3.10
[3] development database: MySql 8.0.0

Second, the project to build


[1], select "New"> "item" from the "File", "menu as shown:

[2] to select "ASP.NET Core Web" application template, and then click "Next." as the picture shows:

[3] Name the project .NetCoreWebApi001, and then click "Create." as the picture shows:

[4] confirm the selection in ASP.NET CORE WEB Applications dialog box .Net Coreand ASP.NET Core 3.0. Select APIa template, and then click 创建. as the picture shows:

[5] Since then, created a .NET Core WebApi.

Third, the test API


Ctrl+F5Run the application. If a message appears asking to be trusted IIS Expressdialog certificate, then select "Yes." In the "Security Warning" dialog box that appears next, select "Yes."

If shown, appear below: prove webApicreated.

Fourth, the basics


【1】appsettings.json

It is the application's configuration file, the equivalent of web.configthe configuration file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": { //例如:连接字符串
    "Mysql": "Data Source=.;Initial Catalog=NetCoreWebApi;User Id=sa;Password=123;"
  }
}

[2] Program.csClass

It is managed entry point of the application.

   //文件是应用的入口, 启动后通过UseStartup<Startup>()指定下文的Startup启动文件进行启动。
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
                {   //创建WebHostBuilder。
                    webBuilder.UseStartup<Startup>(); 
                    //指定启动类,用于依赖注入和中间件注册。
                });
    }
}

[3] Startup.csClass

It is the configuration of services and applications HTTP request pipeline.

    //此文件中可以以包含服务配置、定义请求处理管道的重要操作
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

V. Written in the last


Reference Document 1

Reference Document 2

Guess you like

Origin www.cnblogs.com/ZengJiaLin/p/11931296.html