diff --git a/SwaggerStudy/Controllers/StudentController.cs b/SwaggerStudy/Controllers/StudentController.cs index 8bf11e8..b3dbe6f 100644 --- a/SwaggerStudy/Controllers/StudentController.cs +++ b/SwaggerStudy/Controllers/StudentController.cs @@ -12,8 +12,8 @@ using System.Threading.Tasks; namespace SwaggerStudy.Controllers { - [Route("api/[controller]/[action]")] [ApiController] + [Route("api/[controller]/[action]")] public class StudentController : ControllerBase { private readonly ILogger _logger; @@ -40,6 +40,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("GetAllAsync")] public async Task GetAllAsync() { var apiResult = new ResultBase() @@ -53,7 +54,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] - public IActionResult Get(int studentId) + public IActionResult Get(int studentId) { var apiResult = new ResultBase() { @@ -66,6 +67,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("GetAsync")] public async Task GetAsync(int studentId) { var apiResult = new ResultBase() @@ -92,6 +94,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("GetByNameAsync")] public async Task GetByNameAsync(string studentName) { var apiResult = new ResultBase() @@ -104,7 +107,6 @@ namespace SwaggerStudy.Controllers return await Task.FromResult(Ok(apiResult)); } - [HttpGet] public IActionResult Add(StudentVModel vm) { @@ -139,6 +141,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("AddAsync")] public async Task AddAsync(StudentVModel vm) { var apiResult = new ResultBase() @@ -205,6 +208,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("UpdateAsync")] public async Task UpdateAsync(StudentVModel vm) { var apiResult = new ResultBase() @@ -264,6 +268,7 @@ namespace SwaggerStudy.Controllers } [HttpGet] + [ActionName("DeleteAsync")] public async Task DeleteAsync(int studentId) { var apiResult = new ResultBase() diff --git a/SwaggerStudy/Properties/launchSettings.json b/SwaggerStudy/Properties/launchSettings.json index 42a3ad7..1ce2f1c 100644 --- a/SwaggerStudy/Properties/launchSettings.json +++ b/SwaggerStudy/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "Swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -21,7 +21,7 @@ "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "Swagger/", "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/SwaggerStudy/Startup.cs b/SwaggerStudy/Startup.cs index de62501..21ed704 100644 --- a/SwaggerStudy/Startup.cs +++ b/SwaggerStudy/Startup.cs @@ -11,7 +11,14 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Swashbuckle; +using Swashbuckle.AspNetCore.Swagger; +using Swashbuckle.AspNetCore.SwaggerGen; +using Swashbuckle.AspNetCore.SwaggerUI; + + using SwaggerStudy.Services; +using Microsoft.OpenApi.Models; namespace SwaggerStudy { @@ -26,12 +33,19 @@ namespace SwaggerStudy public void ConfigureServices(IServiceCollection services) { - services.AddControllers() + services + .AddControllers() .AddJsonOptions(jsonOption=> { jsonOption.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter()); jsonOption.JsonSerializerOptions.Encoder= System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All); }); + #region swagger + services.AddSwaggerGen(setup => + { + setup.SwaggerDoc("GroupA", new OpenApiInfo { Title = "Swagger学习", Version = "第一版" }); + }); + #endregion services.AddTransient(); } @@ -43,6 +57,13 @@ namespace SwaggerStudy app.UseDeveloperExceptionPage(); } + app.UseSwagger(); + app.UseSwaggerUI(setup => + { + setup.SwaggerEndpoint("/swagger/GroupA/swagger.json", "Swagger学习第一版"); + + }); + app.UseRouting(); app.UseAuthorization(); diff --git a/SwaggerStudy/SwaggerStudy.csproj b/SwaggerStudy/SwaggerStudy.csproj index 7b21126..6765bf5 100644 --- a/SwaggerStudy/SwaggerStudy.csproj +++ b/SwaggerStudy/SwaggerStudy.csproj @@ -14,4 +14,8 @@ + + + +