在 ResponseHeader 中保留自定义 header

Persist the custom header in ResponseHeader

我所有的 MVC 控制器都继承自一个基本控制器,它有一个方法可以在响应 header:

中添加我需要的 header
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{    
    filterContext.HttpContext.Response.AddHeader("AdditionaInfo", Environment.MachineName);
}

这在我的本地环境中运行良好,但在将其部署到 Azure 后,我没有看到此 header 响应。

我可以在响应中看到其他标准 headers:

Cache-Control: private, s-maxage=0

Content-Encoding: gzip Content-Length: 122 Content-Type: application/json; charset=utf-8

Date: Server: Microsoft-IIS/10.0

Strict-Transport-Security: max-age=300

Vary: Accept-Encoding

X-AspNet-Version: 4.0.30319

X-AspNetMvc-Version: 5.2

X-Powered-By:ASP.NET

只是我的 header 在响应中丢失了。

我需要在 Azure 中配置什么吗?还是添加 header 的方式导致了问题?

我会考虑将代码移动到中间件而不是过滤器(我假设您是根据 OnActionExecuted 方法名称来做的)。

您可以在 startup.cs

中以简单的方式完成此操作
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // other code here

    app.Use(async (context, next) =>
    {
        context.Response.Headers.Add("AdditionalInfo", Environment.MachineName);
        await next.Invoke();
    });

    // additional code here

    app.UseMvc();
}