C# 在 ASP.NET 上创建 JSON 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/894828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:44:39  来源:igfitidea点击:

Creating a JSON Header on ASP.NET

c#phpasp.netjsonheader

提问by

I am converting a script from PHP to ASP.net C#. In PHP, i could use something like:

我正在将脚本从 PHP 转换为 ASP.net C#。在 PHP 中,我可以使用类似的东西:

header('Content-type: text/json');

header('内容类型:文本/json');

header('Content-type: application/json');

header('内容类型:应用程序/json');

How can I tell my aspx page to declare in the header that it is printing a JSON file?

如何告诉我的 aspx 页面在标题中声明它正在打印 JSON 文件?

采纳答案by JerSchneid

Response.ContentType = "application/json";

or more generally

或更一般地

Response.Headers.Add("Content-type", "text/json");
Response.Headers.Add("Content-type", "application/json");

回答by dvdmn

Additional info about JerSchneid's answer

关于 JerSchneid 回答的附加信息

If you got an error message like this:

如果您收到这样的错误消息:

This operation requires IIS integrated pipeline mode.

此操作需要 IIS 集成管道模式。

You can use this way:

你可以这样使用:

Response.AddHeader("Content-type", "text/json");