// 处理默认路由以及区域路由问题
private static bool ResolveAreasSupportByRouteConstraint(ApiDescription apiDescription, string targetApiVersion)
{
if (targetApiVersion == "v1")
{
return apiDescription.Route.RouteTemplate.StartsWith("api/{controller}");
}
var routeTemplateStart = "api/" + targetApiVersion;
return apiDescription.Route.RouteTemplate.StartsWith(routeTemplateStart);
}
///
/// 添加 Area 文档过滤器
///
public class AddAreasSupportDocumentFilter:IDocumentFilter
{
///
/// 配置
///
private readonly SwaggerConfiguration _config = ConfigProvider.Default.GetConfiguration();
}
## 4.6 显示上传文件参数 SwaggerUI 有上传文件的功能,与添加自定义`HTTP Header`做法相类似,我们可以通过特性来标识API是否具有上传功能。///
/// 添加 上传操作过滤
///
public class AddUploadOperationFilter:IOperationFilter
{
///
/// 重写Apply方法,加入Upload操作过滤
///
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var upload = apiDescription.ActionDescriptor.GetCustomAttributes().FirstOrDefault();
if (upload == null)
{
return;
}
operation.consumes.Add("application/form-data");
operation.parameters.Add(new Parameter()
{
name = upload.Name,
@in = "formData",
required = upload.Require,
type = "file",
description = upload.Description
});
}
}
///
/// 上传属性,用于标识接口是否包含上传信息参数
///
public class UploadAttribute:Attribute
{
///
/// 参数名
///
public string Name { get; set; } = "file";
}
然后再`SwaggerConfig.cs`的`EnableSwagger`配置类添加一行操作过滤注册代码即可。c.OperationFilter();
效果图如下: ![](https://images2018.cnblogs.com/blog/534030/201806/534030-20180615154052314-1404284569.png) ## 4.7 自定义接口备注内容 由于不想将一些跟接口名无关的信息放在接口名那里,那么这个时候可以将部分业务相关的注释放在`<remarks></remarks>`标签当中///
/// 获取后台Guid
///
///
/// 测试一些内容,不想将无用的东西放在接口名称当中
/// 换行输出一下内容
///
///
[HttpGet][ApiAuthor(Name = "jian玄冰",Status = DevStatus.Wait,Time = "2018-04-28")]
public Guid GetGuid()
{
return Guid.NewGuid();
}
///
/// 获取后台Guid
///
///
/// 测试一些内容,不想将无用的东西放在接口名称当中
/// 换行输出一下内容
///
///
[HttpGet][AllowAnonymous]
[ApiAuthor(Name = "jian玄冰",Status = DevStatus.Wait,Time = "2018-04-28")]
public Guid GetGuid()
{
return Guid.NewGuid();
}