Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON as a .json file

Tags:

json

c#

asp.net

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.

like image 741
Michael Hartmann Avatar asked Dec 07 '25 10:12

Michael Hartmann


2 Answers

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

like image 83
outcoldman Avatar answered Dec 10 '25 01:12

outcoldman


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.

like image 22
Ivaylo Slavov Avatar answered Dec 10 '25 00:12

Ivaylo Slavov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!