Is there a content header type for adding an HttpResponseHeader?

The only method I see in HttpResponseHeaders is Add, which takes a string type for the header type. I just wonder if .NET provided a list of states like HttpResponseHeader in a string?

So, I can do:

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

I see that there is an Enum list in the HttpResponseHeader, but it does not provide a string value ...

ie HttpResponseHeader.ContentRange, but the correct header line should be Content-Range

Correct me if I am wrong ...

+5
source share
5 answers

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

HttpContentHeaders ( Headers System.Net.Http.HttpContent) Content-Type, Content-Length, Content-Encoding .. ( , ).

:

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

... .

+5

, - .

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

webapi :

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

, @ServiceGuy - webapi/mvc

,

+1

WebRequest WebResponse, HttpWebRequest.ContentType.

0

# Enum . :

int val = 5;
HttpResponseHeader header = HttpResponseHeader.Allow 

val.ToString() "5", header.ToString() "Allow".

response.Headers.Add(HttpResponseHeader.ContentRange.ToString(), "something");
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();
0
source

All Articles