我正在將WSON對象發佈到WCF中的REST API。字符串和整型屬性通過很好,但IList類型的屬性爲null,即使它們是在JSON中指定的。JSON對象在WCF REST POST上具有空數組屬性
這裏的JSON對象是什麼樣子,它已經字符串化後:
{
"Id":"1",
"Name":"Group One ertqer",
"Description":"This is group 1 estrfasd",
"SecurityPrincipals":"[
{
"Id": "1",
"DisplayName": "User One",
"UserName": "user.one",
"Sid": "null",
"Fqdn": "gmd.lab"
} ,
{
"Id": "2",
"DisplayName": "User Two",
"UserName": "user.two",
"Sid": "null",
"Fqdn": "gmd.lab"
}
]",
"SecurityPermissions":"[
{
"Id": "2",
"Name": "Access Service Catalog"
} ,
{
"Id": "1",
"Name": "Access Portal"
}
]"
}
這裏是jQuery的AJAX代碼,我使用POST對象:
var securitygroup = {
group: {
Id: $("#csm-security-groupid").val(),
Name: $("#csm-security-groupname").val(),
Description: $('#csm-security-groupdesc').val(),
"SecurityPrincipals[]": principals,
"SecurityPermissions[]": permissions
}
};
$.ajax({
url: 'http://localhost:809/RestDataServices/RestDataService.svc/SecurityGroups/SecurityGroup',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(securitygroup),
dataType: 'json',
type: 'POST',
async: true,
success: function (data, success, xhr) {
alert('Group saved - ' + success);
},
error: function (xhr, status, error) {
alert('Error! - ' + xhr.status + ' ' + error);
}
});
這裏是REST操作合同:
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "SecurityGroups/SecurityGroup")]
void SaveSecurityGroup(SecurityGroup group);
SecurityGroup的數據合同:
[DataContract]
public class SecurityGroup
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public IList<SecurityPermission> SecurityPermissions { get; set; }
[DataMember]
public IList<SecurityPrincipal> SecurityPrincipals { get; set; }
}
最後,這裏的REST服務的web.config文件的相關部分:
<system.serviceModel>
<services>
<service name="RestDataServices.RestDataService" behaviorConfiguration="DefaultBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestDataServices.IRestDataService" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
所以我已經嘗試了一堆不同的東西:
1)嘗試都包裹和解開 2)最初的數據合同有列表<>屬性,而不是IList <>,但很明顯,這並沒有工作 3)我在StackExchange上瀏覽了大約十幾個其他問題,並且他們中沒有一個似乎是相關的vant到我的問題。
我寧願不必創建一個自定義的格式化程序...我猜我只是在做錯誤的事情,結果會變得非常簡單。
對此的任何幫助將不勝感激。我已經花了大約6個小時,我將放棄並將這些陣列本身作爲單獨的調用發佈。