asp.net-mvc mvc 4 web api 添加自定义响应 http 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13487012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:25:49  来源:igfitidea点击:

mvc 4 web api add custom response http header

asp.net-mvcasp.net-mvc-4http-headersasp.net-web-api

提问by Eng Boon

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

我们可以在默认响应标头旁边添加额外的 http 响应标头项示例“Last Updated”吗?

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

例如,当我调用 (Request):
localHost:12345/API/GetInfo
with header:
Host: localHost:12345
......


然后 API 将使用 (Response):
HTTP/1.1 200 OK
Content-Length:回复 header XX
Content-Type: XXX
Last-Update: API 函数生成的值和值

回答by HoberMellow

You can add header by using this code:

您可以使用以下代码添加标题:

HttpContext.Current.Response.AppendHeader("Last-Update", value);

回答by JTech

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

仅供参考,您可以使用官方 HTTP 标头来表示资源上次更新的日期时间。

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

它是“Last-Modified”标头(请参阅规范第 14 节页面上的第 14.29 节)。

You add it to your response like this:

您可以像这样将其添加到您的回复中:

Response.Content.Headers.LastModified = yourResource.LastUpdatedDateTime;

回答by TapiwaJoel Mudavanhu

In MVC 5 just add

在 MVC 5 中只需添加

 Response.AppendHeader("header", "value");

回答by Tola Ch.

I just found a solution. What I need to do is, response HTTP header in cookie format. That way, browser will always return it back to my web server.

我刚刚找到了解决方案。我需要做的是,以 cookie 格式响应 HTTP 标头。这样,浏览器将始终将其返回到我的 Web 服务器。