Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
C#
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

The "content type" should be set by using the existing property like in the next example:
C#
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:
C#
HttpResponse httpResponse = HttpContext.Current.Response;
 
Share this answer
 
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
I have find the solution we can add like this

C#
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);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900