简体   繁体   English

WCF Rest更改根返回元素的名称

[英]WCF Rest Change name of root return element

I have 我有

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "projects/{projectname}")]
[return: MessageParameter(Name = "ProjectId")]
Guid CreateProject(String projectName);

But this still returns 但这仍然返回

<guid 
xmlns="http://schemas.microsoft.com/2003/10/Serialization/">00000000-0000-0000-0000-00000000</guid>

How do I replace "guid" with ProjectId? 如何用ProjectId替换“ guid”?

public Guid CreateProject(String projectName)
{
     return Guid.Empty;
}

If I change the OperationContract BodyStyle to WrappedResponse I get: 如果将OperationContract BodyStyle更改为WrappedResponse, WrappedResponse得到:

<CreateProjectResponse 
    xmlns="http://tempuri.org/">
    <ProjectId>00000000-0000-0000-0000-000000000</ProjectId>
</CreateProjectResponse>

Which is almost what I want, but I don't want unnecessarily wrapped. 这几乎是我想要的,但是我不需要不必要的包装。

You become what you have defined. 您将成为定义的对象。 You have defined to receive empty GUID in XML form and it returns to you empty GUID in XML. 您已定义为接收XML形式的空GUID,并且它将返回给您XML形式的空GUID。 All is right. 没事。

Maybe you need convert GUID to some kind of string id, then Guid.NewGuid().ToString("N") 也许您需要将GUID转换为某种字符串ID,然后再Guid.NewGuid().ToString("N")

When you aren't expecting xml then you need to fit attributes and use HTTP Accept Header from client, for example for Json Accept: application/json , or string - plain/text 当您不期望使用xml时,则需要调整属性并从客户端使用HTTP Accept Header,例如,对于Json Accept: application/json或string- plain/text

UPD: Now a little bit clear. UPD:现在有点清楚了。 Your actually ask how to change XML structure. 您实际上问过如何更改XML结构。 I recommend you for standard types use standard XML structure because you already have implemented from the box XML formatters. 我建议您为标准类型使用标准XML结构,因为您已经从框XML格式器中实现了。 Anyway when you need to change formatters, you can do it by extending the WebHttpBehavior and overriding the WebHttpBehavior.GetReplyDispatchFormatter method to return our own custom implementation of System.ServiceModel.Dispatcher.IDispatchFormatter (as example read here ) 无论如何,当您需要更改格式化程序时,都可以通过扩展WebHttpBehavior并重写WebHttpBehavior.GetReplyDispatchFormatter方法来返回我们自己的System.ServiceModel.Dispatcher.IDispatchFormatter自定义实现(例如, 在此处阅读)

I want also just mention, that you are using WCF 4 REST, and this technology is legacy. 我还想提一提,您正在使用WCF 4 REST,而这项技术是旧的。 When you are dealing not with legacy project or maintenance, then I recommend you to use ASP.NET Web API , because this and many other things could be done there much more easier. 当您不处理遗留项目或维护时,我建议您使用ASP.NET Web API ,因为这样做和执行许多其他操作会更容易。

Easiest solution is to 最简单的解决方案是

return new XElement("ProjectId", Guid.Empty);

Not really what I wanted, but it works. 并不是我真正想要的,但是可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM