WCF REST服务:请求对象为空

时间:2014-03-12 09:51:39

标签: .net wcf rest

我试图将对象发送到我的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等类似问题而没有运气

由于

2 个答案:

答案 0 :(得分:0)

尝试更改您发布的数据,如下所示:

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

我似乎记得当我使用WCF休息时,我从来没有在HTTP请求中指定变量名。

答案 1 :(得分:0)

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