ASP.NET Core uses Swagger

1. New project: dotnet new mvc -n SwaggerTest

 

 

 

2. Add nuget reference: dotnet add TodoApi.csproj package Swashbuckle.AspNetCore -v 5.0.0

You can also use the Package Manager Console

 

 

 Three. ConfigureServices in Startup to add services

  services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Docs", Version = "V1" });
            });

 

 

 Four. Configure the code in Startup is as follows

            app.UseSwagger();

            app.UseSwaggerUI(c=>c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

 

 

 Five. Add the following code in HomeController

    public class HomeController : Controller
    {
        /// <param name="name"></param>        
        [HttpPost("{name}")]
        public IActionResult Find(string name)
        {
            if (string.IsNullOrWhiteSpace(name)) return NotFound();
            else return Content(name);
        }
    }

6. Test

 

 

 

Guess you like

Origin www.cnblogs.com/vic-tory/p/12713378.html