C# ASP.Core Web API swagger environment configuration

This article's code warehouse address: gitee code cloud CSDN notes warehouse address


1. New project

[File]-[New Project]-[ASP.Core Web]-[API]
Insert picture description here
Insert picture description here

Second, install swagger

[Tools]-[NuGet Package Manager]-[Package Manager Console]-Then enter in the console:
Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4
Insert picture description here
Insert picture description here

3. Reference services in Startup.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace WebApplication1 {
    
    
  public class Startup {
    
    
    public Startup(IConfiguration configuration)
    {
    
    
      Configuration = configuration;
    }

    public IConfiguration Configuration {
    
     get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
    
    
      // 1.安装:Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4
      // 2-1.注册服务
      services.AddSwaggerGen(m =>
      {
    
    
        m.SwaggerDoc("v1", new OpenApiInfo {
    
     Title = "WebApplication1", Version = "v1"});
      });
      services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    
    
      if (env.IsDevelopment())
      {
    
    
        app.UseDeveloperExceptionPage();
      }

      app.UseRouting();

      app.UseAuthorization();

      // 2-2.引用服务
      app.UseSwagger();
      app.UseSwaggerUI(m => {
    
    
        m.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1");
        }
      );
      // 3.启动【地址:http://localhost:58464/swagger/index.html】
      // 这里到这里环境就已经配置好了,有需要可以继续下面的
      // 4-1.修改默认启动页【Properties 下的 launchSettings.json】

      app.UseEndpoints(endpoints =>
      {
    
    
        endpoints.MapControllers();
      });
    }
  }
}

Fourth, modify the default startup page

{
    
    
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    
    
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
    
    
      "applicationUrl": "http://localhost:58464",
      "sslPort": 0
    }
  },
  "profiles": {
    
    
    "IIS Express": {
    
    
      "commandName": "IISExpress",
      "launchBrowser": true,
      // "launchUrl": "weatherforecast",
      // 4-2.在这里修改默认启动页
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
    
    
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication1": {
    
    
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
    
    
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Take a few notes so you can read them later.

Guess you like

Origin blog.csdn.net/qq_44111597/article/details/109157949