Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc 4 web api add custom response http header

Can we add the extra http response header item example "Last Updated" beside the default response header?

example when I call (Request):
localHost:12345/API/GetInfo
with header:
Host: localHost:12345
......


then the api will reply the header with(Response):
HTTP/1.1 200 OK
Content-Length: XX
Content-Type: XXX
Last-Update: The value and the value generate from the API function

like image 531
Eng Boon Avatar asked Nov 21 '12 05:11

Eng Boon


People also ask

How do I add HTTP response header?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can you add custom HTTP headers?

If you want to set a custom HTTP headers on your server to be sent in your HTTP responses, the process is quite simple. We have outlined below the snippets required to add a custom header for both Apache and Nginx web servers.

Can we set response header?

You can set response headers, you can add response headers. And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.

Does HTTP response have headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.


2 Answers

You can add header by using this code:

HttpContext.Current.Response.AppendHeader("Last-Update", value);
like image 156
HoberMellow Avatar answered Sep 28 '22 04:09

HoberMellow


FYI there is an official HTTP Header that you can use to represent the DateTime a resource was last updated.

It is the 'Last-Modified' Header (See section 14.29 on Section 14 page of the specification).

You add it to your response like this:

Response.Content.Headers.LastModified = yourResource.LastUpdatedDateTime;
like image 32
JTech Avatar answered Sep 28 '22 03:09

JTech