[英]WCF Rest Change name of root return element
我有
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "projects/{projectname}")]
[return: MessageParameter(Name = "ProjectId")]
Guid CreateProject(String projectName);
但這仍然返回
<guid
xmlns="http://schemas.microsoft.com/2003/10/Serialization/">00000000-0000-0000-0000-00000000</guid>
如何用ProjectId替換“ guid”?
public Guid CreateProject(String projectName)
{
return Guid.Empty;
}
如果將OperationContract BodyStyle更改為WrappedResponse, WrappedResponse
得到:
<CreateProjectResponse
xmlns="http://tempuri.org/">
<ProjectId>00000000-0000-0000-0000-000000000</ProjectId>
</CreateProjectResponse>
這幾乎是我想要的,但是我不需要不必要的包裝。
您將成為定義的對象。 您已定義為接收XML形式的空GUID,並且它將返回給您XML形式的空GUID。 沒事。
也許您需要將GUID轉換為某種字符串ID,然后再Guid.NewGuid().ToString("N")
當您不期望使用xml時,則需要調整屬性並從客戶端使用HTTP Accept Header,例如,對於Json Accept: application/json
或string- plain/text
UPD:現在有點清楚了。 您實際上問過如何更改XML結構。 我建議您為標准類型使用標准XML結構,因為您已經從框XML格式器中實現了。 無論如何,當您需要更改格式化程序時,都可以通過擴展WebHttpBehavior
並重寫WebHttpBehavior.GetReplyDispatchFormatter
方法來返回我們自己的System.ServiceModel.Dispatcher.IDispatchFormatter自定義實現(例如, 在此處閱讀)
我還想提一提,您正在使用WCF 4 REST,而這項技術是舊的。 當您不處理遺留項目或維護時,我建議您使用ASP.NET Web API ,因為這樣做和執行許多其他操作會更容易。
最簡單的解決方案是
return new XElement("ProjectId", Guid.Empty);
並不是我真正想要的,但是可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.