3

Aspx ファイルを返す代わりに、次のコードを json ファイルとして返そうとしています。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 ファイルとして認識されるようにしたいだけです。

4

3 に答える 3

5

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

于 2013-05-13T18:32:47.697 に答える
4

使用してみてください:

// 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構成で構成します:

<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 です)。ただし、コンテンツ タイプ ヘッダーを応答に含める必要があります。

于 2013-05-13T18:29:26.620 に答える
2

「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());
于 2013-05-13T18:33:17.627 に答える