KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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> As you might imagine, this should export item 2. So far so good, yes? 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); } 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(){...}); 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. QUICK ANSWER: 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 t
Tags (comma-separated)
Save Edits
Cancel