标签:contex res string 标记 rdo mat value http 参数
源码:https://github.com/1259724620/hqh.project20200501.git
一、web项目添加swagger包Swashbuckle.AspNetCore
二、在Web项目模块类WebModule.cs文件添加如下代码
三、在web项目新建文件夹swagger并添加如下几个类
/// <summary> /// 枚举参数示例 /// </summary> public class EnumSchemaFilter : ISchemaFilter { /// <summary> /// 设置枚举显示 /// </summary> /// <param name="schema"></param> /// <param name="context"></param> public void Apply(OpenApiSchema schema, SchemaFilterContext context) { var enumType = context.Type.GetNonNullableType(); if (enumType.IsEnum) { var extensions = new Dictionary<string, IOpenApiExtension>(); foreach (Enum value in Enum.GetValues(enumType)) { var display = value.DisplayName(); extensions.Add(Convert.ToInt32(value).ToString("G"), new OpenApiString(string.IsNullOrWhiteSpace(display) ? "" : ":" + display)); } schema.Extensions = extensions; if (string.IsNullOrWhiteSpace(schema.Description)) { schema.Description = enumType.DisplayDescription(); } } } }
/// <summary> /// 隐藏Swagger Api特性 /// </summary> [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class SwaggerIgnoreAttribute : Attribute { }
/// <summary> /// Swagger Api 过滤器 /// </summary> public class SwaggerIgnoreFilter : IDocumentFilter { /// <summary> /// /// </summary> /// <param name="swaggerDoc"></param> /// <param name="context"></param> public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { var ignoreApis = context.ApiDescriptions.Where(m => m.RelativePath.StartsWith("Abp") || m.RelativePath.StartsWith("api/abp") || (m.TryGetMethodInfo(out MethodInfo methodInfo) && methodInfo.CustomAttributes.Any(info => info.AttributeType == typeof(SwaggerIgnoreAttribute)))); if (ignoreApis != null) { foreach (var ignoreApi in ignoreApis) { swaggerDoc.Paths.Remove("/" + ignoreApi.RelativePath); } } } }
四、修改controller下面的控制器
新建一个BaseController,如下代码,记得添加路由标记属性,否则swagger不会显示
/// <summary> /// 基础控制器 /// </summary> [Route("v1/api/[controller]")] public class BaseController: AbpController { }
修改UserController,HomeController继承为BaseController,如下
/// <summary> /// 用户控制器 /// </summary> public class UserController : BaseController { /// <summary> ///新增或编辑用户 /// </summary> /// <returns></returns> [HttpPost("AddEditUser")] public async Task<Result> AddEditUser([FromBody]AddEditUserDto input, [FromServices]IUserService service) { return await service.AddEditUser(input); } }
public class HomeController: BaseController { /// <summary> /// /// </summary> /// <returns></returns> public IActionResult Index() { return Redirect("~/swagger"); } }
五、设置web项目,dto项目,service项目属性
选中项目-->属性-->生成,然后如下栏位勾选保存即可
六、运行,如下,并调用接口成功
标签:contex res string 标记 rdo mat value http 参数
原文地址:https://www.cnblogs.com/come-on-come-on/p/12809813.html