Question

why Response.AddHeader is used?

Was it helpful?

Solution

It's used to add additional HTTP Headers to your request-- read the previous link if you're unfamiliar wtih what an HTTP Header is used for.

Most of the time, you'll end up setting headers indirectly, using other ASP.NET objects or methods like Response.Cookies or Response.Redirect. However, there are advanced, relatively unusual scenarios where it's sometimes necessary to call Response.AddHeader() directly in your code.

For example, to cause an HTTP 301 (permanent) redirect in ASP.NET 3.5, you'd need to use Response.AddHeader, using code like this:

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

OTHER TIPS

Just one example of Justin Grant's answer is if you want to output excel you can do the following:

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=test.xls");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top