【发布时间】:2011-07-01 23:29:43
【问题描述】:
如果我用 PHP 中的纯文本响应 http 请求,我会这样做:
<?php
header('Content-Type: text/plain');
echo "This is plain text";
?>
我将如何在 ASP.NET 中做同样的事情?
【问题讨论】:
标签: asp.net http web-applications
如果我用 PHP 中的纯文本响应 http 请求,我会这样做:
<?php
header('Content-Type: text/plain');
echo "This is plain text";
?>
我将如何在 ASP.NET 中做同样的事情?
【问题讨论】:
标签: asp.net http web-applications
如果您只想返回这样的纯文本,我会使用 ashx 文件(VS 中的通用处理程序)。然后只需在 ProcessRequest 方法中添加您要返回的文本即可。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("This is plain text");
}
这消除了普通 aspx 页面的额外开销。
【讨论】:
您应该使用 Page 类的 Response 属性:
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "text/plain");
Response.Write("This is plain text");
Response.End();
【讨论】:
C# 中的示例(对于 VB.NET,只需删除结尾 ;):
Response.ContentType = "text/plain";
Response.Write("This is plain text");
您可能需要事先调用Response.Clear 以确保缓冲区中已经没有标题或内容。
【讨论】:
<%%>) 中
Response.ContentType = "text/plain";
Response.Write("This is plain text");
【讨论】: