簡體   English   中英

C# HTTP 服務器 POST 請求將正文解析為 null

[英]C# HTTP Server POST request is parsing the body as null

我有我的 ServiceContract 接口

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
         ResponseFormat = WebMessageFormat.Json,
         BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
         UriTemplate = "print")]
    [return: MessageParameter(Name = "Data")]
    int Print(PrintOrder orden);
}

服務:

public class Service : IService
    {
        public int Print(PrintOrder orden)
        {
            try
            {
                Console.WriteLine("received order",orden.Body,orden.PrinterName);
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message,ex.StackTrace);
                return 1;
            }
        }
    }

和數據合同

[DataContract]
    public class PrintOrder
    {
        [DataMember]
        public string PrinterName{ get; set; }
        [DataMember]
        public string Body { get; set; }
    }

問題是每次我發送帶有 JSON 正文的 POST 請求時,我的服務方法Print總是接收 null PrintOrder作為參數:

{
  "PrinterName":"EPSON 1200",
  "Body":"test body"
}

BodyStyle = WebMessageBodyStyle.WrappedRequest,更改為BodyStyle = WebMessageBodyStyle.Bare,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM