|
|
@ -10,16 +10,18 @@ using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
|
|
|
using Microsoft.OpenApi.Interfaces;
|
|
|
|
|
|
|
|
using Microsoft.OpenApi.Any;
|
|
|
|
|
|
|
|
|
|
|
|
using Swashbuckle;
|
|
|
|
using Swashbuckle;
|
|
|
|
|
|
|
|
using Swashbuckle.AspNetCore;
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
|
|
|
|
|
|
|
|
|
|
using SwaggerStudy.Services;
|
|
|
|
using SwaggerStudy.Services;
|
|
|
|
using Microsoft.OpenApi.Interfaces;
|
|
|
|
|
|
|
|
using Microsoft.OpenApi.Any;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace SwaggerStudy
|
|
|
|
namespace SwaggerStudy
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -39,15 +41,20 @@ namespace SwaggerStudy
|
|
|
|
{
|
|
|
|
{
|
|
|
|
services
|
|
|
|
services
|
|
|
|
.AddControllers()
|
|
|
|
.AddControllers()
|
|
|
|
|
|
|
|
//配置System.Text.Json选项
|
|
|
|
.AddJsonOptions(jsonOption =>
|
|
|
|
.AddJsonOptions(jsonOption =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//整齐打印:压缩格式(一行)还是整齐格式(换行)
|
|
|
|
jsonOption.JsonSerializerOptions.WriteIndented = true;
|
|
|
|
jsonOption.JsonSerializerOptions.WriteIndented = true;
|
|
|
|
|
|
|
|
//允许(忽略)对象或数据末尾多余的逗号
|
|
|
|
jsonOption.JsonSerializerOptions.AllowTrailingCommas = true;
|
|
|
|
jsonOption.JsonSerializerOptions.AllowTrailingCommas = true;
|
|
|
|
|
|
|
|
//自定义时间格式转换器
|
|
|
|
jsonOption.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
|
|
|
|
jsonOption.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
|
|
|
|
|
|
|
|
//转义字符串编码器:直接显示中文
|
|
|
|
jsonOption.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All);
|
|
|
|
jsonOption.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//注入SwaggerGen
|
|
|
|
//注册Swagger生成器,定义一个或多个 Swagger 文档
|
|
|
|
services.AddSwaggerGen(setup =>
|
|
|
|
services.AddSwaggerGen(setup =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//注意:name参数即是DocumentName, 在SwaggerUi中有使用,配置的终结点里必须保持一致
|
|
|
|
//注意:name参数即是DocumentName, 在SwaggerUi中有使用,配置的终结点里必须保持一致
|
|
|
@ -152,6 +159,9 @@ namespace SwaggerStudy
|
|
|
|
//swagger 访问页面前缀:默认swagger
|
|
|
|
//swagger 访问页面前缀:默认swagger
|
|
|
|
//SwaggerUi首页地址:baseUrl+前辍+index.html 默认:/swagger/index.html
|
|
|
|
//SwaggerUi首页地址:baseUrl+前辍+index.html 默认:/swagger/index.html
|
|
|
|
setup.RoutePrefix = "Swagger";
|
|
|
|
setup.RoutePrefix = "Swagger";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//必须包含{documentName}:即是SwaggerDoc的name参数.
|
|
|
|
|
|
|
|
//默认为/swagger/{documentName}/swagger.json
|
|
|
|
setup.SwaggerEndpoint("/WebApi/swagger/DemoName/swagger.json", "WebApi 测试版");
|
|
|
|
setup.SwaggerEndpoint("/WebApi/swagger/DemoName/swagger.json", "WebApi 测试版");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|