zoukankan      html  css  js  c++  java
  • .NET WCF Return String 字符串有反斜杠的处理

     

    应该是:

    {"Message":"Hello World"}

    结果是:"

    {"Message":"Hello World"}"

    正确的写法是:

    [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);
    }


    主要提示内容是:

    I finally figured out a solution to this. It's not what I would have preferred (which would be to return the specific object type, and somehow instruct WCF to 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 be 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/json", so I set it explicitly.

    Because this uses HttpContext, I'm guessing it will only work if you have [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] on your service class, since that will force requests to the service to go through the ASP.NET pipeline. Without the asp.net compatibility, the HttpContext will not be available, since wcf hosting is supposed to be host agnostic.

    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 what obstacles I might run into regarding *de*serialization, when my service methods have [DataContract] object types as input parameters. I'm assuming the DataContractJsonSerializer will be used for that too. Will cross that bridge when I come to it...if it creates a problem. It hasn't so far, with my simple DTOs.

    UPDATE See Oleg's answer (the UPDATE2 part). He changes the return type of the service method from void to System.ServiceModel.Channels.Message, 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 way of 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 done any specific tests, but it's possible that this would be 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.

    原文链接是:

    http://stackoverflow.com/questions/3026934/how-can-i-return-json-from-my-wcf-rest-service-net-4-using-json-net-without

  • 相关阅读:
    AndroidApplication Fundamentals(Android应用基础)
    当汇错款时该怎么办?
    VS2005控制台程序修改nb0文件
    nand flash 扇区的管理以及初始化
    Androidz之Activity概要学习
    Android开发者必备的42个链接
    Android Activity形象描述
    一个前端妹子的悲欢编程之路
    提高工作效率的几个小技巧
    前端几个常用简单的开发手册拿走不谢
  • 原文地址:https://www.cnblogs.com/rock_chen/p/5546896.html
Copyright © 2011-2022 走看看