2014-03-12 142 views
0

我试图将对象发送给我的REST服务,但对象未正确映射。WCF 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"}} 

函数被调用,数据对象是存在的,但它的属性为null

我一直在寻找到没有运气像Sending JSON to WCF Rest Service - object is always nullhow to send custom object to WCF REST Service in browser based url类似的问题

感谢

+0

你能否提供Models.Notification类?你的电话对我来说好。 –

+0

刚刚更新了问题。谢谢 – GLlompart

+0

这对我来说很奇怪,你是否试图通过.net调用你的方法?我通过使用HttpWebRequest或WebClient。 –

回答

0

尝试改变您发布的数据是这样的:

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

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

0

我认为改变WebMessageBodyStyle.BareWebMessageBodyStyle.Wrapped可以在你的情况下做到这一点。