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