English 中文(简体)
Changing JSON response root node message in WCF
原标题:

I created a REST GetTime Service in WCF and the service returns JSON as the response message. Also the WebMessageBodyStyle is set to wrapped so it would have an ID associated with that data it returns. But when I use Fiddler to test my service the response string is:

{"GetTimeResult":"2010614104013"}

As the response above the ID of the string is GetTimeResult, I m wondering is there any way on changing that bit of test to timestamp. So it looks like this:

{"timestamp":"2010614104013"}

Cheers.

最佳回答

If you are using the DataContract/DataMember attributes in your code, you add a name (as well as some other named parameters).

[DataMember(Name = "timestamp")]
public string GetTimeResult
问题回答

As pointed out in this article, suppose you are not using a data member explicitly in a data contract and want to return, say, a timestamp as a simple string for the response. Just use the annotation [return: MessageParameter(Name = "timestamp")] with your operation contract method:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/timestamps", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[return: MessageParameter(Name = "timestamp")]
string GetStringTimestamp();




相关问题
IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

ASP.net web services

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/...

Unity Container Disposing and XML Web Service

I am registering some wrapers over un-managed objects in container. How can I dispose of them at the end of the container s lifetime? Please bear in mind I have an XML Web service.

SharePoint : web service permission error

I have a sharepoint site, and I am calling a standard sharepoint web service. I create the web service request like this : wsDws.Url = this.SiteAddress + @"/_vti_bin/Dws.asmx"; When I use ...

热门标签