为什么Response.AddHeader使用?

有帮助吗?

解决方案

它是用来添加额外的 HTTP头您request--阅读以前的链接,如果您“再陌生wtih什么的HTTP标头的用途。

大多数时候,你会最终间接设定标头,使用其他ASP.NET物体或类似Response.Cookies或Response.Redirect的方法。不过,也有先进的,比较不寻常的场景中有时需要调用Response.AddHeader()直接在你的代码。

例如,以引起HTTP 301(永久)在ASP.NET 3.5重定向,你需要使用Response.AddHeader,使用这样的代码:

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location","/newpage.aspx");
}
</script>

其他提示

贾斯汀·格兰特的回答只是一个例子是,如果你想输出Excel您可以执行以下操作:

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=test.xls");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top