-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3deb4f
commit 32bc0fe
Showing
6 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Web.Http.Description; | ||
using Swashbuckle.Application; | ||
using SwashbuckleEx.WebApiTest; | ||
using SwashbuckleEx.WebApiTest.Extensions; | ||
using SwashbuckleEx.WebApiTest.Selectors; | ||
|
||
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] | ||
|
@@ -20,7 +21,17 @@ public static void Register() | |
GlobalConfiguration.Configuration | ||
.EnableSwagger(c => | ||
{ | ||
//c.SingleApiVersion("v1", "Test.WebApi"); | ||
// 配置简单API文档信息-用于单个文档 | ||
//c.SingleApiVersion("v1", "Test.WebApi").Contact(x => | ||
//{ | ||
// x.Email("[email protected]"); | ||
// x.Name("jian玄冰"); | ||
// x.Url("https://www.cnblogs.com/jianxuanbing"); | ||
//}).TermsOfService("jian玄冰1").License(x => | ||
//{ | ||
// x.Name("MIT"); | ||
// x.Url("https://www.cnblogs.com/jianxuanbing"); | ||
//}).Description("自定义文案内容,可以随便输入内容"); | ||
c.MultipleApiVersions(ResolveAreasSupportByRouteConstraint, (vc) => | ||
{ | ||
vc.Version("Admin", "中文后台 API").Description("这个用于测试一下备注信息").TermsOfService("www.baidu.com").License( | ||
|
@@ -33,10 +44,11 @@ public static void Register() | |
{ | ||
x.Name("2017").Email("[email protected]").Url("www.baidu.xxxx"); | ||
}); | ||
vc.Version("v1", "Common API",true); | ||
vc.Version("Client", "Client API"); | ||
vc.Version("v1", "Common API", true); | ||
|
||
vc.Version("Client", "Client API"); | ||
}); | ||
|
||
c.ApiKey("Authorization").Description("OAuth2 Auth").In("header").Name("Authorization"); | ||
//c.OAuth2("jwt").AuthorizationUrl("http://localhost:9460/oauth/token") | ||
// .TokenUrl("http://localhost:9460/oauth/token").Scopes( | ||
|
@@ -45,6 +57,9 @@ public static void Register() | |
// x.Add("scope", "admin"); | ||
// }); | ||
c.DocumentFilter<SwaggerAreasSupportDocumentFilter>(); | ||
|
||
c.OperationFilter<AddUploadOperationFilter>(); | ||
|
||
c.IncludeXmlComments(string.Format("{0}/bin/SwashbuckleEx.WebApiTest.XML", AppDomain.CurrentDomain.BaseDirectory)); | ||
c.ShowDeveloperInfo(); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Http.Description; | ||
using Swashbuckle.Swagger; | ||
|
||
namespace SwashbuckleEx.WebApiTest.Extensions | ||
{ | ||
/// <summary> | ||
/// 添加 上传操作过滤 | ||
/// </summary> | ||
public class AddUploadOperationFilter : IOperationFilter | ||
{ | ||
/// <summary> | ||
/// 重写Apply方法,加入Upload操作过滤 | ||
/// </summary> | ||
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) | ||
{ | ||
var upload = apiDescription.ActionDescriptor.GetCustomAttributes<UploadAttribute>().FirstOrDefault(); | ||
if (upload == null) | ||
{ | ||
return; | ||
} | ||
operation.consumes.Add("application/form-data"); | ||
if (operation.parameters==null) | ||
{ | ||
operation.parameters = new List<Parameter>(); | ||
} | ||
operation.parameters.Add(new Parameter() | ||
{ | ||
name = upload.Name, | ||
@in = "formData", | ||
required = upload.Require, | ||
type = "file", | ||
description = upload.Description | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace SwashbuckleEx.WebApiTest.Extensions | ||
{ | ||
/// <summary> | ||
/// 上传属性,用于标识接口是否包含上传信息参数 | ||
/// </summary> | ||
public class UploadAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 参数名 | ||
/// </summary> | ||
public string Name { get; set; } = "file"; | ||
|
||
/// <summary> | ||
/// 是否必须包含文件 | ||
/// </summary> | ||
public bool Require { get; set; } = true; | ||
|
||
/// <summary> | ||
/// 备注 | ||
/// </summary> | ||
public string Description { get; set; } = ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
|
||
namespace SwashbuckleEx.WebApiTest.Models | ||
{ | ||
/// <summary> | ||
/// 用户信息 | ||
/// </summary> | ||
public class UserInfo | ||
{ | ||
/// <summary> | ||
/// 名称 | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// 手机号码 | ||
/// </summary> | ||
public string Phone { get; set; } | ||
|
||
/// <summary> | ||
/// 头像 | ||
/// </summary> | ||
public string Avactor { get; set; } | ||
|
||
/// <summary> | ||
/// 账号 | ||
/// </summary> | ||
public string Account { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters