Skip to content

Commit d776260

Browse files
committed
增加text/json和text/xml响应类型的支持
1 parent 3684cf9 commit d776260

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

WebApiClientCore/Attributes/ReturnAttributes/JsonReturnAttribute.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Http.Headers;
1+
using System;
2+
using System.Net.Http.Headers;
23
using System.Threading.Tasks;
34
using WebApiClientCore.HttpContents;
45

@@ -26,6 +27,23 @@ public JsonReturnAttribute(double acceptQuality)
2627
{
2728
}
2829

30+
/// <summary>
31+
/// 指示响应的ContentType与AcceptContentType是否匹配
32+
/// 返回false则调用下一个ApiReturnAttribute来处理响应结果
33+
/// </summary>
34+
/// <param name="responseContentType">响应的ContentType</param>
35+
/// <returns></returns>
36+
protected override bool IsMatchAcceptContentType(MediaTypeHeaderValue? responseContentType)
37+
{
38+
var result = base.IsMatchAcceptContentType(responseContentType);
39+
if (result == false)
40+
{
41+
var mediaType = responseContentType?.MediaType;
42+
result = string.Equals(mediaType, "text/json", StringComparison.OrdinalIgnoreCase);
43+
}
44+
return result;
45+
}
46+
2947
/// <summary>
3048
/// 设置强类型模型结果值
3149
/// </summary>

WebApiClientCore/Attributes/ReturnAttributes/XmlReturnAttribute.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Http.Headers;
1+
using System;
2+
using System.Net.Http.Headers;
23
using System.Threading.Tasks;
34
using WebApiClientCore.HttpContents;
45

@@ -26,6 +27,23 @@ public XmlReturnAttribute(double acceptQuality)
2627
{
2728
}
2829

30+
/// <summary>
31+
/// 指示响应的ContentType与AcceptContentType是否匹配
32+
/// 返回false则调用下一个ApiReturnAttribute来处理响应结果
33+
/// </summary>
34+
/// <param name="responseContentType">响应的ContentType</param>
35+
/// <returns></returns>
36+
protected override bool IsMatchAcceptContentType(MediaTypeHeaderValue? responseContentType)
37+
{
38+
var result = base.IsMatchAcceptContentType(responseContentType);
39+
if (result == false)
40+
{
41+
var mediaType = responseContentType?.MediaType;
42+
result = string.Equals(mediaType, "text/xml", StringComparison.OrdinalIgnoreCase);
43+
}
44+
return result;
45+
}
46+
2947
/// <summary>
3048
/// 设置强类型模型结果值
3149
/// </summary>

0 commit comments

Comments
 (0)