【问题标题】:Returning JSON as a .json file将 JSON 作为 .json 文件返回
【发布时间】:2013-05-13 18:22:58
【问题描述】:

我试图让以下代码作为 json 文件返回,而不是返回 Aspx 文件。 我在 PHP 中使用了类似的代码,但是我不能在 C# 中复制它。我希望它以 .json 文件的形式响应。

 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();

输出没问题,我只是想让它被识别为 .json 文件。

【问题讨论】:

  • 将其写入文件。 System.IO.File.WriteAllText (@"D:\path.txt", json);
  • @RobertHarvey 我认为他希望返回内容类型为 json 的 页面,而不是实际将其写入文件...不过可能是错误的。
  • 您是说要将.json 添加到托管文件扩展名列表中吗?那么GET foo.json 命中托管管道和 .NET 堆栈?
  • 没错。我不希望它存储在文件中。流量会大,怕txt文件搞混,发错数据。

标签: c# asp.net json


【解决方案1】:

要启动“文件下载”对话框,您应该尝试Content-disposition,我不知道它如何与json 一起使用,但当我将它用于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();

这是一个指向 msdn 文档http://support.microsoft.com/kb/260519的链接

【讨论】:

    【解决方案2】:

    尝试使用:

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

    甚至更好:

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

    对于您似乎在做的事情,我建议使用 IHttpHandler 而不是 aspx 页面。您甚至可以将其配置为具有 json 扩展名(尽管扩展名不应该那么重要)。这是一个例子:

    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
        }
    

    并在 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>
    

    您可以使用动词来使 GET/POST 或其他请求类型可以访问句柄,对所有请求都使用 "*"。该处理程序可作为“~/CustomHttpHandler.json”访问 - 请注意添加的 json 扩展名(原始文件为 .ashx)。不过,您仍然必须将内容类型标头放在响应中。

    【讨论】:

      【解决方案3】:

      您可以使用“Content-Disposition”标头设置返回数据的文件名。

      见: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());
      

      【讨论】:

      • 应该适用于 inline=false。使用 inline=true 时,某些浏览器(我在看你,IE)在你执行 Ctrl-S 时似乎会忽略文件名。
      • 谢谢,我可以将它用于系统的不同部分,他们将以这种方式下载文件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2020-07-16
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      相关资源
      最近更新 更多