2012-11-12 49 views
0

我正在將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個小時,我將放棄並將這些陣列本身作爲單獨的調用發佈。

回答

1

OK,得到的答案是,我需要做的兩件事情是不同的。

1)我已經爲我的JSON數組賦值添加了引號和方括號,因爲我得到了一個400 - 沒有它們的錯誤請求。這是我的空數組/列表屬性的來源。

var securitygroup = { 
    group: { 
     Id: $("#csm-security-groupid").val(), 
     Name: $("#csm-security-groupname").val(), 
     Description: $('#csm-security-groupdesc').val(), 
     "SecurityPrincipals[]": principals, //bad 
     "SecurityPermissions[]": permissions //bad 
    } 
}; 

2)當我刪除了 「」 和[]在SecurityPrincipals和SecurityPermissions性質,我拿到了400 - 再次錯誤的請求。我注意到我的整型屬性在它們周圍有引號,所以我強迫它們是使用parseInt的整數。我也強制「空」到一個適當的空值。以下是JSON構建代碼的外觀:

var principals = []; 
$("li[data-type='permission']").each(function() { 
    principals.push(
     { 
      Id: parseInt(this.id), 
      DisplayName: this.getAttribute('data-name'), 
      UserName: this.getAttribute('data-username'), 
      Sid: this.getAttribute('data-sid') == "null" ? null : this.getAttribute('data-sid'), 
      Fqdn: this.getAttribute('data-fqdn') 
     } 
    ); 
}); 

//permissions for the current group 
var permissions = []; 
$("input[type='checkbox']").each(function() { 
    if (this.getAttribute("checked") == "checked") { 
     permissions.push(
      { 
       Id: parseInt(this.getAttribute('data-id')), 
       Name: this.getAttribute('data-name') 
      } 
     ); 
    } 
}); 

var securitygroup = { 
    group: { 
     Id: parseInt($("#csm-security-groupid").val()), 
     Name: $("#csm-security-groupname").val(), 
     Description: $('#csm-security-groupdesc').val(), 
     SecurityPrincipals: principals, 
     SecurityPermissions: permissions 
    } 
}; 
相關問題