Xml 如何防止JSON web服务响应下载到文件?

Xml 如何防止JSON web服务响应下载到文件?,xml,json,wcf,web-services,rest,Xml,Json,Wcf,Web Services,Rest,我有一些几乎相同的WCF服务代码,除了一个方法应该返回xml结果,另一个是json结果: [ServiceContract] public interface IRestServiceImpl { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,

我有一些几乎相同的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不一样?

由浏览器决定如何处理任何特定的内容类型,听起来您的浏览器不知道如何处理json

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

Content-type: application/json
如果您这样做,问题在于您的浏览器。如果您看不到这一点,那么您的服务器或服务代码是错误的(尤其是如果它是
应用程序/octet流
,这是等同于“我不知道”的mime类型)


至于浏览器,我个人推荐Chrome+这个优秀的插件来显示格式化的JSON输出:

由浏览器决定如何处理任何特定的内容类型,听起来你的浏览器不知道如何处理JSON

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

Content-type: application/json
如果您这样做,问题在于您的浏览器。如果您看不到这一点,那么您的服务器或服务代码是错误的(尤其是如果它是
应用程序/octet流
,这是等同于“我不知道”的mime类型)


至于浏览器,我个人推荐Chrome+这个优秀的插件,用于显示格式化的JSON输出:

这取决于您使用哪个浏览器与服务对话。IE(直到IE9)知道如何显示XML,但不知道如何显示JSON,这就是为什么它显示一个,但要求您保存另一个(就像浏览某个二进制文件时一样)。Chrome知道如何显示这两者,因此您可以浏览到…/json/2468和…/xml/2468,它将显示响应。服务部门对此没有办法


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();
    }
}
公共类StackOverflow_12940785
{
[服务合同]
公共接口IRestServiceImpl
{
[经营合同]
[WebGet(
ResponseFormat=WebMessageFormat.Xml,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“xml/{id}”)]
字符串XMLData(字符串id);
[经营合同]
[WebGet(
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“json/{id}”)]
字符串JSONData(字符串id);
[经营合同]
[WebGet(
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“html/{id}”)]
流HTMLData(字符串id);
}
公共类服务:IRestServiceImpl
{
公共字符串XMLData(字符串id)
{
返回“您要求的产品”+id;
}
公共字符串JSONData(字符串id)
{
返回“您要求的产品”+id;
}
公共流HTMLData(字符串id)
{
WebOperationContext.Current.OutgoingResponse.ContentType=“text/html”;
字符串响应=@“
我的服务
您请求了产品“+id+@”

"; 返回新的MemoryStream(Encoding.UTF8.GetBytes(response)); } } 公共静态无效测试() { string baseAddress=“http://“+Environment.MachineName+”:8000/服务”; WebServiceHost主机=新的WebServiceHost(类型(服务),新的Uri(基地址)); host.Open(); Console.WriteLine(“主机已打开”); 控制台。写入(“按ENTER键关闭主机”); Console.ReadLine(); host.Close(); } }
这取决于您使用哪个浏览器与服务通话。IE(直到IE9)知道如何显示XML,但不知道如何显示JSON,这就是为什么它显示一个,但要求您保存另一个(就像浏览某个二进制文件时一样)。Chrome知道如何显示这两者,因此您可以浏览到…/json/2468和…/xml/2468,它将显示响应。服务部门对此没有办法


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();
    }
}
公共类StackOverflow_12940785
{
[服务合同]
公共接口IRestServiceImpl
{
[经营合同]
[WebGet(
ResponseFormat=WebMessageFormat.Xml,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“xml/{id}”)]
字符串XMLData(字符串id);
[经营合同]
[WebGet(
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“json/{id}”)]
字符串JSONData(