强制ASP.net Web服务返回JSON [英] Forcing ASP.net webservice to return JSON

查看:63
本文介绍了强制ASP.net Web服务返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于Web应用程序的ASP.net Web服务,该服务根据我调用的函数向我返回XML或JSON数据.到目前为止,这一直很好,但是我遇到了一个问题.我想在我的页面上创建一个导出"链接,该链接将下载一个JSON文件.链接的格式非常简单:

I have an ASP.net web service that I'm using for a web application which returns a either XML or JSON data to me, depending on the function I call. This has been working well thus far, but I've run into a problem. I want to create an "export" link on my page that will download a JSON file. The link is formatted very simply:

<a href="mywebserviceaddress/ExportFunc?itemId=2">Export This Item</a>

您可能会想,这应该导出项目2.到目前为止,很好,是吗?

As you might imagine, this should export item 2. So far so good, yes?

问题是,由于我没有特别要求接受的内容类型是JSON,因此ASP.net绝对拒绝发回除XML以外的任何内容,这不适用于这种情况.代码基本上如下:

Problem is that since I'm not specifically requesting that the accepted content type is JSON, ASP.net absolutely refuses to send back anything but XML, which just isn't appropriate for this situation. The code is essentially as follows:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Item ExportItem(int itemId)
    {
        Context.Response.AddHeader("content-disposition", "attachment; filename=export.json"); //Makes it a download

        return GetExportItem(itemId);
    }

尽管我将ResponseFormat指定为JSON,但除非我通过AJAX(使用Google Web Toolkit,BTW)请求此方法,否则我总是获取XML:

Despite my specifying the ResponseFormat as JSON, I always get back XML unless I request this method via AJAX (using Google Web Toolkit, BTW):

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "mywebserviceaddress/ExportFunc");
    builder.setHeader("Content-type","application/json; charset=utf-8");
    builder.setHeader("Accepts","application/json");
    builder.sendRequest("{\"itemId\":2}", new RequestCallback(){...});

那太好了,但是AJAX不会给我一个下载对话框.有什么方法可以强迫ASP.net返还给我JSON,而无论如何请求数据?在我看来,没有对此行为的手动替代就是对设计的全面监督.

That's great, but AJAX won't give me a download dialog. Is there any way to force ASP.net to give me back JSON, regardless of how the data is requested? It would seem to me that not having a manual override for this behavior is a gross design oversight.

快速解答:

首先,我要说的是,我认为womp的答案可能是长期发展的更好方法(转换为WCF),但是deostroll使我想到了我将在不久的将来使用的答案.另外,应该指出的是,这似乎主要是因为我只需要下载,可能无法在所有情况下都能正常工作.无论如何,这是我最终用来获得所需结果的代码:

First off, let me say that I think that womp's answer is probably the better way to go long term (Convert to WCF), but deostroll led me to the answer that I'll be using for the immediate future. Also, it should be noted that this seems to work primarily because I wanted just a download, may not work as well in all situations. In any case, here's the code that I ended up using to get the result I wanted:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void ExportItem(int itemId)
    {
        Item item = GetExportItem(itemId);            

        JavaScriptSerializer js = new JavaScriptSerializer();
        string str = js.Serialize(item);

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-disposition", "attachment; filename=export.json");
        Context.Response.AddHeader("content-length", str.Length.ToString());
        Context.Response.Flush();
        Context.Response.Write(str);
    }

请注意 void 的返回类型(这意味着您的WDSL在该功能旁边将无效).返回任何东西都会搞砸正在手工构建的响应.

Please note the return type of void (which means that your WDSL will be next to useless for this function). Returning anything will screw up the response that is being hand-built.

推荐答案

有两个论坛主题供您参考:

Here are two forums threads for your reference:

http://forums.asp.net/t/1118828.aspx

http://forums.asp.net/p/1054378/2338982 .aspx#2338982

我不清楚.他们说专注于将内容类型设置为application/json.我以前没有使用过wcf,但是我认为您可以使用Response对象.

I have no clear idea. They say on concentrating on setting the content type to application/json. I haven't worked with wcf before, but I think you can make use of the Response object.

在响应对象上设置内容类型.执行response.write将您的json数据作为字符串传递,然后执行response.end.

Set the content type on the response object. Do a response.write passing your json data as string and then do a response.end.

这篇关于强制ASP.net Web服务返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆