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測試

 


 

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