在输出json时对@variable的响应.Write

时间:2013-03-20 21:16:25

标签: asp.net ajax json asp.net-webpages

ASP.NET Web Pages 2示例api.cshtml文件:

此代码返回由jquery $ .ajax调用识别的正确json:

@{
    var data1 = AppFunctions.PerformSelect(AppState["U1select"].ToString());
    var jsondata = Json.Encode(data1);
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(jsondata);
}

但事实并非如此。为什么呢?

@{
    var data1 = AppFunctions.PerformSelect(AppState["U1select"].ToString());
    var jsondata = Json.Encode(data1);
    Response.ContentType = "application/json; charset=utf-8";
}
@jsondata

1 个答案:

答案 0 :(得分:1)

我认为这与Razor有关,而不是json输出问题 - 请参阅this answer

您使用Response.Write(jsondata)还是Html.Raw(jsondata)而不是Html.Output(jsondata)