public AddHeader ( string name, string value ) : void | ||
name | string | |
value | string | |
return | void |
using System.Web; public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.AddHeader("Cache-Control", "no-cache"); context.Response.Write("Response content"); } }
using System.Web; using System.IO; public class MyHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", "inline; filename=myfile.csv"); var data = GetCsvData(); context.Response.BinaryWrite(data); } private byte[] GetCsvData() { var csvContent = "Name,Age,Email\nJohn,30,[email protected]\nJane,25,[email protected]"; using (var memoryStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memoryStream)) { streamWriter.Write(csvContent); streamWriter.Flush(); return memoryStream.ToArray(); } } }In this example, the AddHeader method is used to set the "Content-Disposition" header to "inline; filename=myfile.csv". This will prompt the client to download the response as a file named "myfile.csv". The response content is generated as a CSV file containing some sample data. Package library: System.Web, System.IO