【问题标题】:Returning a string containing valid Json with Nancy使用 Nancy 返回包含有效 Json 的字符串
【发布时间】:2011-11-27 15:54:46
【问题描述】:

我从另一个服务收到一个包含有效 JSON 的字符串。 我只想将这个字符串转发给 Nancy,但也将内容类型设置为“application/json”,这将使我无需在客户端使用 $.parseJSON(data)。

如果我使用 Response.AsJson,它似乎会破坏字符串中的 JSON 并添加转义字符。 我可以使用字符串创建一个 Stream 并将响应类型设置为:

Response test = new Response();
test.ContentType = "application/json";
test.Contents = new MemoryStream(Encoding.UTF8.GetBytes(myJsonString)); 

但想知道是否有更简单的方法?

【问题讨论】:

    标签: c# json nancy


    【解决方案1】:

    如果您的模块的所有路由都返回 JSON 字符串,那么您可以在 After 挂钩中为所有路由一次性设置内容类型:

    Get["/"] = _ =>
    {
        // ... 
        return myJsonString;
    };
    
    After += ctx =>
    {
        ctx.Response.ContentType = "application/json";
    };
    

    【讨论】:

      【解决方案2】:

      这也有效:

      Response.AsText(myJsonString, "application/json");
      

      【讨论】:

        【解决方案3】:

        看起来南希有一个不错的 Response.AsJson 扩展方法:

        Get["/providers"] = _ =>
                    {
                        var providers = this.interactiveDiagnostics
                                            .AvailableDiagnostics
                                            .Select(p => new { p.Name, p.Description, Type = p.GetType().Name, p.GetType().Namespace, Assembly = p.GetType().Assembly.GetName().Name })
                                            .ToArray();
        
                        return Response.AsJson(providers);
                    };
        

        【讨论】:

        • 请注意,如果您打算使用非默认 JSON 序列化程序,则需要正确设置 JsonSerializer 才能使此解决方案正常工作。我更喜欢 Steven Robbins 的回答。
        • 附录:Nancy github.com/NancyFx/Nancy.Serialization.JsonNet 使用 Newtonsoft 序列化程序的说明
        【解决方案4】:

        我喜欢你认为应该有更好的方法,因为你必须使用 3 行代码,我认为这说明了南希 :-)

        我想不出一个“更好”的方法来做,你可以用 GetBytes 的方式来做:

        Get["/"] = _ =>
            {
                var jsonBytes = Encoding.UTF8.GetBytes(myJsonString);
                return new Response
                    {
                        ContentType = "application/json",
                        Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length)
                    };
            };
        

        或者“投一个字符串”的方式:

        Get["/"] = _ =>
            {
                var response = (Response)myJsonString;
        
                response.ContentType = "application/json";
        
                return response;
            };
        

        两者都做同样的事情 - 后者代码更少,前者更具描述性(imo)。

        【讨论】:

        • 很高兴看到这两个选项。
        【解决方案5】:

        几乎是你做的方式。你可以这样做

        var response = (Response)myJsonString;
        response.ContentType = "application/json";
        

        您可以在 IResponseFormatter 上创建一个扩展方法并提供您自己的 AsXXXX 助手。随着 0.8 版本的发布,它自身的响应将会有一些扩展,因此您可以执行 WithHeader(..)、WithStatusCode() 等操作-

        【讨论】:

          猜你喜欢
          • 2011-03-13
          • 2016-11-20
          • 2012-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-03
          • 1970-01-01
          相关资源
          最近更新 更多