1

我有一些几乎相同的 WCF 服务代码,除了一个方法应该返回一个 xml 结果,另一个是一个 json 结果:

[ServiceContract]
public interface IRestServiceImpl
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "xml/{id}")]
    string XMLData(string id);

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/{id}")]
    string JSONData(string id);
}

xml 工作正常(例如,当我在浏览器中输入“ http://localhost:4841/RestServiceImpl.svc/xml/2468 ”时)。

但是,当我输入“ http://localhost:4841/RestServiceImpl.svc/json/2468 ”时,我会看到一个“文件下载 - 安全警告”对话框,它允许我保存一个文件(在这种情况下名为“2468”) ,在记事本中打开时包含以下内容:

{"JSONDataResult":"您请求的产品 2468"}

这是“按设计”(将 json 结果保存到文件),还是为什么它的行为方式与 xml-o-rama 不同?

4

2 回答 2

5

由您的浏览器决定如何处理任何特定的 Content-Type,听起来您的浏览器不知道如何处理 JSON。

要检查这一点,在使用 Chrome 开发控制台或 Firebug(或您使用的任何浏览器中的任何等效项)时,请在加载该资源时查看网络请求。在标题中,您应该会看到类似

Content-type: application/json

如果你这样做,问题是你的浏览器。如果您没有看到,那么您的服务器或服务代码是错误的(特别是如果它application/octet-stream是相当于“我不知道”的 mime 类型)。

至于浏览器,我个人推荐 Chrome + 这个用于显示格式化 JSON 输出的优秀插件:https ://github.com/callumlocke/json-formatter

于 2012-10-17T18:29:32.737 回答
1

这取决于您使用哪个浏览器与服务通信。IE(最高 IE9)知道如何显示 XML,但不知道 JSON,这就是为什么它显示一个但要求您保存另一个(就像您浏览某些二进制文件时一样)。Chrome 知道如何显示两者,因此您可以浏览到 .../json/2468 和 .../xml/2468 ,它会显示响应。服务中没有什么可以做的1


1实际上,如果您感兴趣的是在浏览器中显示输出(而不是某些需要理解 JSON 或 XML 的客户端使用服务),您也可以返回 HTML 格式的数据。一种简单的方法是将 HTML 作为流返回,如下所示。

public class StackOverflow_12940785
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLData(string id);

        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONData(string id);

        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "html/{id}")]
        Stream HTMLData(string id);
    }
    public class Service : IRestServiceImpl
    {
        public string XMLData(string id)
        {
            return "You requested product " + id;
        }

        public string JSONData(string id)
        {
            return "You requested product " + id;
        }

        public Stream HTMLData(string id)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            string response = @"<html>
                <head><title>My service</title></head>
                <body>
                    <p>You requested <b>product " + id + @"</b></p>
                </body>
            </html>";
            return new MemoryStream(Encoding.UTF8.GetBytes(response));
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
于 2012-10-17T18:29:43.080 回答