作爲WCF新手,我試圖找出從WCF服務返回JSON對象的正確配置。WCF響應中的JSON格式
結果我得到的是(在螢火蟲看):
{"TestServiceResult": "{\"AccountID\":999999,\"CardNumber\":555555,\"AccountBalance\":999.99,\"GivenName\":\"Ben\",\"FamilyName\":\"Rosniak\"}"}
我感興趣的部分是一個長字符串,而不是JSON對象我之後。
<!-- Added for Mobile Pay Service-->
<behaviors>
<serviceBehaviors>
<behavior name="MobilePayServiceBehaviour" >
<serviceDebug includeExceptionDetailInFaults="true"/>
<!--<serviceCredentials >
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MobilePayService.WtfUserNamePasswordValidator, MobilePayService" />
</serviceCredentials>
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="MobilePayService.WtfAuthorizationPolicy, MobilePayService" />
</authorizationPolicies>
</serviceAuthorization>-->
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttpBehaviour">
<webHttp automaticFormatSelectionEnabled="false" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
,我使用,以確保響應被格式化的測試方法:
[WebGet(UriTemplate = "TestService/{id}/{device}/{culture}")]
public string TestService(string id, string device, string culture)
{
WCFProfileModel profileModel = new WCFProfileModel()
{
AccountID = 999999,
AccountBalance = 999.99F,
CardNumber = 555555,
GivenName = "Ben",
FamilyName = "Rosniak"
};
return profileModel;
}
關於服務的唯一配置(該項目是由別人開始)不知何故,響應被包裝在某種模板中,我想知道這是怎麼發生的,但我不確定從哪裏開始尋找這個。我想除掉那些"TestServiceResult"
部分,只回:
{"AccountID":999999,"CardNumber":555555,"AccountBalance":999.99,"GivenName":"Ben","FamilyName":"Rosniak"}
UPDATE: 我試圖教程here(更新我的代碼,以反映這一點)以下的,但我得到一個錯誤「的說法profileModel不能隱式轉換爲字符串「。
M $應該默認它爲Bare,但事實並非如此。如果沒有設置,JSON響應會停止工作。 – Devela 2012-10-12 07:13:46