public HttpResponseMessage Get() { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent("Hello World!"); response.Headers.CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = TimeSpan.FromSeconds(60) }; return response; }
public HttpResponseMessage GetFile() { string filePath = "C:\\files\\example.jpg"; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePath, FileMode.Open); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "example.jpg" }; return response; }This example returns a binary response message with status code 200 (OK), and a jpeg file as its content. The "Content-Disposition" header is set to "attachment", meaning the file should be downloaded as an attachment rather than displayed in the browser. Package/Library: HttpResponseMessage is a class in the System.Net.Http namespace, which is part of the .NET framework. No additional packages or libraries are required.