10

The only method I see in HttpResponseHeaders is Add which takes string type for header type. I just wonder did .NET provided a list of HttpResponseHeader type contants in string?

So I can do:

HttpResponseMessage response = Request.CreateResponse.........;
response.Headers.Add(xxxxx.ContentRange, "something");

I can see there is a list of Enum in HttpResponseHeader, but it doesn't provide string value conrespondingly...

i.e HttpResponseHeader.ContentRange, but the correct header string should be Content-Range

Correct me if I am wrong...

Sean
  • 60,939
  • 11
  • 97
  • 136
King Chan
  • 4,212
  • 10
  • 46
  • 78

5 Answers5

11

There are three strongly-typed HTTP header classes in the System.Net.Http.Headers namespace:

HttpContentHeaders (which is accessible via the Headers property of any of the System.Net.Http.HttpContent types) has pre-defined properties for Content-Type, Content-Length, Content-Encoding etc... (which seem to be the headers you were after).

You can set them like this:

var content = new StringContent("foo");
content.Headers.Expires = DateTime.Now.AddHours(4);
content.Headers.ContentType.MediaType = "text/plain";

...and the header names will be set correctly.

mthierba
  • 5,587
  • 1
  • 27
  • 29
1

Maybe you can try something like this.

var whc = new System.Net.WebHeaderCollection();
whc.Add(System.Net.HttpResponseHeader.ContentRange, "myvalue");
Response.Headers.Add(whc);

In a webapi context, the last line could be :

HttpContext.Current.Response.Headers.Add(whc);

anyway, @ServiceGuy's answer is the way to go in a webapi/mvc context

Hope this will help

jbl
  • 15,179
  • 3
  • 34
  • 101
  • The HttpResponseHeaders.Add only accept (string,string) and (string,IEnumerable)... Deosn't accept WebHeaderCollection itself... – King Chan Mar 11 '13 at 17:33
0

You can get and set common headers by property on WebRequest and WebResponse, like HttpWebRequest.ContentType.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

C# has a built-in conversion from Enum to string. Consider these assignments:

int val = 5;
HttpResponseHeader header = HttpResponseHeader.Allow 

Now, val.ToString() will return the string "5", but header.ToString() will return "Allow". So I humbly suggest using

response.Headers.Add(HttpResponseHeader.ContentRange.ToString(), "something");
Christopher Stevenson
  • 2,843
  • 20
  • 25
  • 1
    enum ContentRange to string is going to be "ContentRange" as well, but the correct Header is "Content-Range", that's what I was stated in the question. – King Chan Mar 11 '13 at 17:36
  • Hmm. Is the header correctly formatted anyways? (looking at the request in something like [Fiddler](http://www.fiddler2.com/fiddler2/) – Christopher Stevenson Mar 11 '13 at 17:39
  • It looks like there's come special considerations for Range and Content-Range headers. [Here's some reading material](http://www.piotrwalat.net/file-download-service-with-resume-support-using-asp-net-web-api/) – Christopher Stevenson Mar 11 '13 at 18:12
0

Use the ContentType Property: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype.aspx.

I was able to do this in my WCF service implementation by doing the following:

// Get the outgoing response portion of the current context
var response = WebOperationContext.Current.OutgoingResponse;

// Add ContentType header that specifies we are using JSON
response.ContentType = new MediaTypeHeaderValue("application/json").ToString();
John Meyer
  • 2,296
  • 1
  • 31
  • 39