-
Notifications
You must be signed in to change notification settings - Fork 444
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
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
WebApiClient/Attributes/ParameterAttributes/JsonFormFieldAttribute.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,36 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using WebApiClient.Contexts; | ||
|
||
namespace WebApiClient.Attributes | ||
{ | ||
/// <summary> | ||
/// 表示参数值序列化为Json并作为x-www-form-urlencoded的字段 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] | ||
public class JsonFormFieldAttribute : Attribute, IApiParameterAttribute, IIgnoreWhenNullable | ||
{ | ||
/// <summary> | ||
/// 获取或设置当值为null是否忽略提交 | ||
/// 默认为false | ||
/// </summary> | ||
public bool IgnoreWhenNull { get; set; } | ||
|
||
/// <summary> | ||
/// 执行前 | ||
/// </summary> | ||
/// <param name="context">上下文</param> | ||
/// <param name="parameter">参数</param> | ||
/// <returns></returns> | ||
public async Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) | ||
{ | ||
if (this.IsIgnoreWith(parameter) == false) | ||
{ | ||
var options = context.HttpApiConfig.FormatOptions; | ||
var json = context.HttpApiConfig.JsonFormatter.Serialize(parameter.Value, options); | ||
var fieldName = parameter.Name; | ||
await context.RequestMessage.AddFormFieldAsync(fieldName, json); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
WebApiClient/Attributes/ParameterAttributes/JsonMulitpartTextAttribute.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,37 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using WebApiClient.Contexts; | ||
|
||
namespace WebApiClient.Attributes | ||
{ | ||
/// <summary> | ||
/// 表示参数值序列化为Json并作为multipart/form-data表单的一个文本项 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] | ||
public class JsonMulitpartTextAttribute : Attribute, IApiParameterAttribute, IIgnoreWhenNullable | ||
{ | ||
/// <summary> | ||
/// 获取或设置当值为null是否忽略提交 | ||
/// 默认为false | ||
/// </summary> | ||
public bool IgnoreWhenNull { get; set; } | ||
|
||
/// <summary> | ||
/// 执行前 | ||
/// </summary> | ||
/// <param name="context">上下文</param> | ||
/// <param name="parameter">参数</param> | ||
/// <returns></returns> | ||
public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) | ||
{ | ||
if (this.IsIgnoreWith(parameter) == false) | ||
{ | ||
var options = context.HttpApiConfig.FormatOptions; | ||
var json = context.HttpApiConfig.JsonFormatter.Serialize(parameter.Value, options); | ||
var fieldName = parameter.Name; | ||
context.RequestMessage.AddMulitpartText(fieldName, json); | ||
} | ||
return ApiTask.CompletedTask; | ||
} | ||
} | ||
} |
Binary file not shown.