应该是:
{"Message":"Hello World"}
The result is:"
{\"Message\":\"Hello World\"}"
The correct wording is:
[WebGet(UriTemplate = "hello")]public void SayHello(){ SimpleMessage message = new SimpleMessage() {Message = "Hello World"}; string json = JsonConvert.Serialize(message); HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; HttpContext.Current.Response.Write(json);}
The main tips are:
I finally figured out a solution to this. It's not what I would has preferred (which would is to return the specific object type, and somehow instruct WCF-use a Json.NET serializer, instead of the DataContractJsonSerializer), but it is working great, and it's simple and clear.
Extending my contrived example using this new solution:
[WebGet(UriTemplate = "hello")]public void SayHello(){ SimpleMessage message = new SimpleMessage() {Message = "Hello World"}; string json = JsonConvert.Serialize(message); HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; HttpContext.Current.Response.Write(json);}
Note The return type of void
. We do not return anything, since it would is serialized with DataContractJsonSerializer. Instead, I write directly to the response output stream. Since The return type is void, the processing pipeline doesn ' t set the Content-type to the default type of "Application/js On ', so I set it explicitly.
Because this uses HttpContext
, I ' m guessing it would only work if you had on [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
the your service class, since that would force req Uests to the service to go through the ASP. Pipeline. Without the ASP. Compatibility, the HttpContext won't be available, since WCF hosting are supposed to be host Agnosti C.
Using This method, the results look perfect in firebug for GET requests. Correct Content-type, Correct content length, and raw json, not wrapped in quotes. And, I ' m getting the serialization I want using Json.NET. Best of both worlds.
I ' m not 100% positive of the what obstacles I might run to regarding *de*serialization when my service methods has [DataCo Ntract] Object types as input parameters. I ' m assuming the datacontractjsonserializer would be used for that too. Would cross the bridge when I come to it...if it creates a problem. It hasn ' t far, with my simple DTOs.
UPDATE See Oleg ' s answer (the UPDATE2 part). He changes the return type of the service method from void System.ServiceModel.Channels.Message
to, and rather than using HttpContext.Current.Response.Write()
, he uses:
return WebOperationContext.Current.CreateTextResponse (json, "application/json; charset=utf-8", Encoding.UTF8);
Which is indeed a better solution. Thank you Oleg.
UPDATE 2 There is yet another the accomplishing this. Change your service's return type from Message to Stream, and return this:
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";return new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
I Haven ' t do any specific tests, but it's possible that this would is a better choice for methods that could potentially Return large amounts of data. I don ' t know if that matters for non-binary data though. Anyway, a thought.
The original link is:
Http://stackoverflow.com/questions/3026934/how-can-i-return-json-from-my-wcf-rest-service-net-4-using-json-net-without
. NET WCF Return string string with backslash handling