Skip to content

Commit

Permalink
增加上传过滤操作
Browse files Browse the repository at this point in the history
  • Loading branch information
jianxuanbing committed Jun 15, 2018
1 parent f3deb4f commit 32bc0fe
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
23 changes: 19 additions & 4 deletions SwashbuckleEx.WebApiTest/App_Start/SwaggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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();
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swashbuckle.Swagger.Annotations;
using SwashbuckleEx.WebApiTest.Extensions;
using SwashbuckleEx.WebApiTest.Models;

namespace SwashbuckleEx.WebApiTest.Areas.Admin.Controllers
{
Expand All @@ -20,13 +24,46 @@ public TestAController()
/// <summary>
/// 获取后台Guid
/// </summary>
/// <remarks>
/// 测试一些内容,不想将无用的东西放在接口名称当中<br/>
/// 换行输出一下内容
/// </remarks>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
[ApiAuthor(Name = "jian玄冰",Status = DevStatus.Wait,Time = "2018-04-28")]
public Guid GetGuid()
{
return Guid.NewGuid();
}

/// <summary>
/// 上传文件
/// </summary>
[HttpPost]
[Upload]
public void UploadFile()
{

}

/// <summary>
/// 查看API开发状态
/// </summary>
[HttpGet]
[ApiAuthor(Name = "jian玄冰", Status = DevStatus.Wait, Time = "2018-04-28")]
public void ApiStatus()
{

}

/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK,"自定义内容",Type = typeof(UserInfo))]
public HttpResponseMessage GetUserInfo()
{
return Request.CreateResponse(HttpStatusCode.OK, new UserInfo(), "application/json");
}
}
}
38 changes: 38 additions & 0 deletions SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs
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
});
}
}
}
25 changes: 25 additions & 0 deletions SwashbuckleEx.WebApiTest/Extensions/UploadAttribute.cs
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; } = "";
}
}
33 changes: 33 additions & 0 deletions SwashbuckleEx.WebApiTest/Models/UserInfo.cs
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; }
}
}
5 changes: 4 additions & 1 deletion SwashbuckleEx.WebApiTest/SwashbuckleEx.WebApiTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<UseGlobalApplicationHostFile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<Use64BitIISExpress />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -141,15 +142,18 @@
<Compile Include="Areas\Client\Controllers\TestAController.cs" />
<Compile Include="Areas\Extensions.cs" />
<Compile Include="Controllers\TestAController.cs" />
<Compile Include="Extensions\AddUploadOperationFilter.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\UserInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Selectors\AreaHttpControllerSelector.cs" />
<Compile Include="Selectors\ClassifiedHttpControllerSelector.cs" />
<Compile Include="Selectors\ControllerTypeSpecifications.cs" />
<Compile Include="Selectors\NamespaceHttpControllerSelector.cs" />
<Compile Include="Selectors\SwaggerAreasSupportDocumentFilter.cs" />
<Compile Include="Extensions\UploadAttribute.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -163,7 +167,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.Core\Swashbuckle.Core.csproj">
Expand Down

0 comments on commit 32bc0fe

Please sign in to comment.