添加:swagger

控制器同时存在同步与异步API会报错
master
bicijinlian 4 years ago
parent d9c571a6fd
commit af707427da

@ -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<StudentController> _logger;
@ -40,6 +40,7 @@ namespace SwaggerStudy.Controllers
}
[HttpGet]
[ActionName("GetAllAsync")]
public async Task<IActionResult> 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<IActionResult> GetAsync(int studentId)
{
var apiResult = new ResultBase()
@ -92,6 +94,7 @@ namespace SwaggerStudy.Controllers
}
[HttpGet]
[ActionName("GetByNameAsync")]
public async Task<IActionResult> 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<IActionResult> AddAsync(StudentVModel vm)
{
var apiResult = new ResultBase()
@ -205,6 +208,7 @@ namespace SwaggerStudy.Controllers
}
[HttpGet]
[ActionName("UpdateAsync")]
public async Task<IActionResult> UpdateAsync(StudentVModel vm)
{
var apiResult = new ResultBase()
@ -264,6 +268,7 @@ namespace SwaggerStudy.Controllers
}
[HttpGet]
[ActionName("DeleteAsync")]
public async Task<IActionResult> DeleteAsync(int studentId)
{
var apiResult = new ResultBase()

@ -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"

@ -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<StudentServer>();
}
@ -43,6 +57,13 @@ namespace SwaggerStudy
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(setup =>
{
setup.SwaggerEndpoint("/swagger/GroupA/swagger.json", "Swagger学习第一版");
});
app.UseRouting();
app.UseAuthorization();

@ -14,4 +14,8 @@
<Folder Include="Swagger\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
</Project>

Loading…
Cancel
Save