繁体   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