Michael Hartmann
Michael Hartmann

Reputation: 584

Returning JSON as a .json file

I am trying to get the following code to return as a json file instead of returning a Aspx file. I have used similar code in PHP that works, however, I can not duplicate it in C#. I would like it to respond as a .json file.

 string json = JsonConvert.SerializeObject(output, Formatting.Indented);
        Response.Headers.Add("Content-type", "application/json; charset=utf-8");
        Response.Headers.Add("Expires"," Mon, 26 Jul 1997 05:00:00 GMT"); 
        Response.Headers.Add("Pragma.","no-cache");
        Response.Cache.SetNoStore();

The output is fine, I just want it to be recognized as a .json file.

Upvotes: 3

Views: 3674

Answers (3)

stackh34p
stackh34p

Reputation: 9009

Try using:

// note the case
Response.Headers.Add("Content-Type", "application/json; charset=utf-8"); 

or even better:

Response.ContentType = "application/json; charset=utf-8";

For what you seem to do, I'd recommend using an IHttpHandler instead of aspx page. You can even configure that one to have the json extension (although extension should not be that important). Here is an example:

public class CustomHttpHandler : IHttpHandler
{
    // return true to reuse (cache server-side) the result 
    // for the same request parameters or false, otherwise 
    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext context)
    {
        var response = context.Response;
        // do with the response what you must
    }

and configure it in the web config:

<configuration>
    </system.web>
        <httpHandlers>
            <remove verb="*" path="*.asmx" />
            <add verb="*" 
                 path="*.asmx" 
                 validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions" />
            <add verb="*" 
                 path="*_AppService.axd" 
                 validate="false" 
                 type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions" />
            <add verb="GET,HEAD" 
                 path="ScriptResource.axd" 
                 type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions" validate="false" />
            <!-- your handler here: -->
            <add verb="GET" 
                 path="CustomHttpHandler.json" 
                 type="YourApp.CustomHttpHandler, YourApp" />
        </httpHandlers>
     </system.web>
</configuration>

You can play with the verb to make the handle accessible to GET/POST or other request types, use "*" for all. The handler is accessible as "~/CustomHttpHandler.json" - note the json extension added (the original is .ashx). You still have to put the content type headers in the response though.

Upvotes: 4

Matt Houser
Matt Houser

Reputation: 36073

You can set the filename of the returned data using the "Content-Disposition" header.

See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

var cd = new System.Net.Mime.ContentDisposition
{
  FileName = "MyData.json",

  // always prompt the user for downloading, set to true if you want 
  // the browser to try to show the file inline
  Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());

Upvotes: 2

outcoldman
outcoldman

Reputation: 11832

To raise "File Download" dialog you should try Content-disposition, I don't know how it wil work with json, but it worked fine for me when I used it for pdf.

string json = JsonConvert.SerializeObject(output, Formatting.Indented);
Response.Headers.Add("Content-type", "application/json; charset=utf-8");
Response.Headers.Add("Content-disposition", "attachment;filename=\"a.json\"");
Response.Headers.Add("Expires"," Mon, 26 Jul 1997 05:00:00 GMT"); 
Response.Headers.Add("Pragma.","no-cache");
Response.Cache.SetNoStore();

And this is a link to msdn documentation http://support.microsoft.com/kb/260519

Upvotes: 5

Related Questions