WCF post json and return json

在WCF restful 服务中POST json 动态对象并返回JSON动态对象

参考阅读

(42条消息) 【WCF】WCF RESTful(二)——搭建一个REST的服务_哈士奇-CSDN博客

 

源码

@@@code

[OperationContract]

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]

[SwaggerWcfTag(nameof(GetUTCRange))]

[SwaggerWcfContentTypes(new string[] { "text/plain" })]

public Stream GetUTCRange(Stream streamdata)

{

using (StreamReader reader = new StreamReader(stream))

{

string res = reader.ReadToEnd();

reader.Close();

// return res; //读取动态JSON参数

}

WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; //修改返回的声明

return new JsonResponse() { code = 1, msg = $"invalid tableName {json.tableName}" }.ToStream();

}

public static Stream ToStream(this JsonResponse json, bool none = true)

{

return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json.Serialize(none)));

}

@@#

注意:请求头要设置为text/plain



 

Swagger测试

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章