【问题标题】:C# HTTP Server POST request is parsing the body as nullC# HTTP Server POST 请求将正文解析为 null
【发布时间】:2022-01-06 07:15:32
【问题描述】:

我有我的 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 总是接收一个空的 PrintOrder 作为参数:

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

【问题讨论】:

    标签: c# rest wcf post


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多