【问题标题】:WCF REST Service: Request object is nullWCF REST 服务:请求对象为空
【发布时间】:2014-03-12 09:51:38
【问题描述】:

我正在尝试将一个对象发送到我的 REST 服务,但该对象未正确映射。

我已将我的合同定义为:

<OperationContract(),
    WebInvoke(UriTemplate:="/SendNotification",
              ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare,
              Method:="POST")>
Function SendNotification(ByVal data As Models.Notification) As Boolean

型号:

Namespace Models
    <DataContract()>
    Public Class Notification
        <DataMember(Name:="ApplicationID")>
        Public Property ApplicationID As String
        <DataMember(Name:="PortalIDs")>
        Public Property PortalIDs As String
        <DataMember(Name:="Message")>
        Public Property Message As String
        <DataMember(Name:="Badge")>
        Public Property Badge As String
    End Class
End Namespace

然后我发布我的数据,例如:

var o = {data: { ApplicationID: "2", PortalIDs: "pid", Message: "THis is a test", Badge: "3"}};

$.ajax({
    type: "POST",
    url: url + "/SendNotification",
    contentType:"application/json",
    data: JSON.stringify(o),
    processData : false
}).done(function( msg ) {

});

生成的请求如下:

POST http://localhost:62530/Services/Notification/NotificationWS.svc/SendNotification HTTP/1.1
Host: localhost:62530
Connection: keep-alive
Content-Length: 87
Cache-Control: no-cache
Pragma: no-cache
Origin: http://localhost:62530
X-Requested-With: XMLHttpRequest
Content-Type: application/json
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)         Chrome/33.0.1750.146 Safari/537.36
DNT: 1
Referer: http://localhost:62530/D.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en;q=0.8,ca;q=0.6,fr;q=0.4,pt;q=0.2,es;q=0.2

{"data":{"ApplicationID":"2","PortalIDs":"pid","Message":"THis is a test","Badge":"3"}}

函数被调用,并且有一个数据对象,但是它的属性是空的

我一直在研究类似的问题,例如 Sending JSON to WCF Rest Service - object is always nullhow to send custom object to WCF REST Service in browser based url,但没有运气

谢谢

【问题讨论】:

  • 你能提供 Models.Notification 类吗?你的电话在我看来没问题。
  • 刚刚更新了问题。谢谢
  • 这对我来说很奇怪,您是否尝试过通过.net调用您的方法?我使用 HttpWebRequest 或 WebClient。
  • 没有。我会尝试显示结果
  • 如果成功然后在提琴手中查看调用是如何构造的。我认为这将是一些愚蠢的事情。我以前也遇到过这样的问题,而且总是很傻:)

标签: .net wcf rest


【解决方案1】:

尝试像这样更改您发布的数据:

var o = {ApplicationID: "2", PortalIDs: "pid", Message: "This is a test", Badge: "3"};

我似乎记得,当我使用 WCF 时,我不必在我的 HTTP 请求中指定变量名。

【讨论】:

    【解决方案2】:

    我认为将 WebMessageBodyStyle.Bare 更改为 WebMessageBodyStyle.Wrapped 可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2016-06-15
      • 2016-06-27
      • 2019-03-02
      • 1970-01-01
      • 2011-07-19
      • 2011-09-29
      • 1970-01-01
      • 2019-05-20
      • 2018-05-05
      相关资源
      最近更新 更多