Click here to Skip to main content
14,665,452 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

How to add the Content-Length,Content-Type and Last-Modified to the HttpResponseMessage Header using .net.

I need to append the all these values manually to the response after adding these fields i need to return the response from the server.
I have tried to adding these fields in fallowing way
httpResponse.Content.Headers.Add("Content-Length", item.Size.ToString());
   httpResponse.Content.Headers.Add("Content-Type", item.ContentType);



But it throwing the exception as "Object reference not set to an instance of an object".

If i am adding like this
httpResponse.Headers.Add("Content-Length", item.Size.ToString());
                  httpResponse.Headers.Add("Content-Type", item.ContentType);


I am getting the fallowing error
"Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."

Please any one help me to add these fields to the HttpResponsesMessage .

thanks
purna
Posted
Updated 18-Mar-14 21:39pm
v2

Rate this:
Please Sign up or sign in to vote.

Solution 2

I have find the solution we can add like this

httpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue(item.ContentType);
                  httpResponse.Content.Headers.ContentLength = item.Size;
                  var info = TimeZoneInfo.FindSystemTimeZoneById("UTC");
                  httpResponse.Content.Headers.LastModified = TimeZoneInfo.ConvertTime(item.LastModifiedDate, info);
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

The "content type" should be set by using the existing property like in the next example:
httpResponse.ContentType = "application/vnd.ms-excel";


Other header like adding a file to download should be like in your example:
httpResponse.AddHeader("content-disposition", "attachment; filename=" + this.FileDownloadName);


And your object httpResponse should be used from your ASPX code behind, like in the next example:
HttpResponse httpResponse = HttpContext.Current.Response;
   
v3
Comments
Purna.N 19-Mar-14 3:25am
   
If I am using this I it showing the fallowing exception
"System.Net.Http.HttpResponseMessage' does not contain a definition for 'ContentType' and no extension method 'ContentType' accepting a first argument of type 'System.Net.Http.HttpResponseMessage'
Raul Iloc 19-Mar-14 3:31am
   
See my last update.
Could you give me more details about what are you trying to do?
Purna.N 19-Mar-14 3:37am
   
I need to send the httpresponsemessage with that fields. The values are present in Variables. I need to assign those values to the fields that are mentioned.
Raul Iloc 19-Mar-14 3:39am
   
Are your httpResponse object accesed like I said by using: "HttpContext.Current.Response"
?
Purna.N 19-Mar-14 3:42am
   
I Have used the HttpContext but our senior developers are said use only HTTP response instead of HttpContext
Raul Iloc 19-Mar-14 3:44am
   
Try like I've suggested and should work, at least for "Content Type" !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100