1- using System . Net . Http . Headers ;
2- using System . Threading . Tasks ;
1+ using System . Threading . Tasks ;
32using WebApiClientCore . Extensions . JsonRpc ;
43
54namespace WebApiClientCore . Attributes
65{
76 /// <summary>
87 /// 表示Json-Rpc请求方法
98 /// </summary>
10- public class JsonRpcMethodAttribute : JsonReturnAttribute , IApiActionAttribute , IApiFilterAttribute
9+ public class JsonRpcMethodAttribute : HttpPostAttribute , IApiFilterAttribute
1110 {
1211 /// <summary>
13- /// Post请求特性
12+ /// JsonRpc的方法名称
1413 /// </summary>
15- private HttpPostAttribute postAttribute = new HttpPostAttribute ( ) ;
16-
17- /// <summary>
18- /// 获取JsonRpc的方法名称
19- /// 为null则使用声明的方法名
20- /// </summary>
21- public string ? Method { get ; set ; }
14+ private readonly string ? method ;
2215
2316 /// <summary>
2417 /// 获取或设置JsonRpc的参数风格
@@ -33,25 +26,15 @@ public class JsonRpcMethodAttribute : JsonReturnAttribute, IApiActionAttribute,
3326 public string ContentType { get ; set ; } = JsonRpcContent . MediaType ;
3427
3528 /// <summary>
36- /// 获取或设置JsonRpc的路径
37- /// 可以为空、相对路径或绝对路径
38- /// </summary>
39- public string ? Path
40- {
41- get => this . postAttribute . Path ;
42- set => this . postAttribute = new HttpPostAttribute ( value ) ;
43- }
44-
45- /// <summary>
46- /// 获取顺序排序的索引
29+ /// 获取过滤器是否启用
4730 /// </summary>
48- public override int OrderIndex => this . postAttribute . OrderIndex ;
31+ bool IAttributeEnable . Enable => true ;
4932
5033 /// <summary>
5134 /// Json-Rpc请求方法
5235 /// </summary>
5336 public JsonRpcMethodAttribute ( )
54- : this ( null )
37+ : this ( method : null , path : null )
5538 {
5639 }
5740
@@ -60,28 +43,37 @@ public JsonRpcMethodAttribute()
6043 /// </summary>
6144 /// <param name="method">JsonRpc的方法名称</param>
6245 public JsonRpcMethodAttribute ( string ? method )
63- : base ( )
46+ : this ( method , path : null )
6447 {
65- this . Method = method ;
6648 }
6749
50+ /// <summary>
51+ /// Json-Rpc请求方法
52+ /// </summary>
53+ /// <param name="method">JsonRpc的方法名称</param>
54+ /// <param name="path">JsonRpc路径</param>
55+ public JsonRpcMethodAttribute ( string ? method , string ? path )
56+ : base ( path )
57+ {
58+ this . method = method ;
59+ }
6860
6961 /// <summary>
7062 /// Action请求前
7163 /// </summary>
72- /// <param name="context">上下文 </param>
64+ /// <param name="context"></param>
7365 /// <returns></returns>
74- Task IApiActionAttribute . OnRequestAsync ( ApiRequestContext context )
66+ public override Task OnRequestAsync ( ApiRequestContext context )
7567 {
7668 var jsonRpcContext = new JsonRpcContext
7769 {
7870 MediaType = this . ContentType ,
79- Method = this . Method ?? context . ApiAction . Name ,
71+ Method = this . method ?? context . ApiAction . Name ,
8072 ParamsStyle = this . ParamsStyle
8173 } ;
8274
8375 context . Properties . Set ( typeof ( JsonRpcContext ) , jsonRpcContext ) ;
84- return this . postAttribute . OnRequestAsync ( context ) ;
76+ return base . OnRequestAsync ( context ) ;
8577 }
8678
8779 /// <summary>
@@ -104,15 +96,5 @@ Task IApiFilterAttribute.OnResponseAsync(ApiResponseContext context)
10496 {
10597 return Task . CompletedTask ;
10698 }
107-
108- /// <summary>
109- /// 不验证响应的ContentType
110- /// </summary>
111- /// <param name="responseContentType"></param>
112- /// <returns></returns>
113- protected override bool IsMatchAcceptContentType ( MediaTypeHeaderValue ? responseContentType )
114- {
115- return true ;
116- }
11799 }
118100}
0 commit comments