【问题标题】:Persist the custom header in ResponseHeader在 ResponseHeader 中保留自定义标头
【发布时间】:2019-06-16 12:38:57
【问题描述】:

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

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{    
    filterContext.HttpContext.Response.AddHeader("AdditionaInfo", Environment.MachineName);
}

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

我可以在响应中看到其他标准标头,例如:

缓存控制:私有,s-maxage=0

内容编码:gzip 内容长度:122 内容类型:应用程序/json;字符集=utf-8

日期:服务器:Microsoft-IIS/10.0

严格传输安全性:max-age=300

变化:接受编码

X-AspNet-版本:4.0.30319

X-AspNetMvc-版本:5.2

X-Powered-By:ASP.NET

只是响应中缺少我的标题。

我需要在 Azure 中进行一些配置吗?还是添加标题的方式导致问题?

【问题讨论】:

    标签: c# azure-web-app-service response-headers


    【解决方案1】:

    我会考虑将代码移动到中间件而不是过滤器(这是我假设您根据 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();
    }
    

    【讨论】:

    • 是的,你是对的,我正在过滤器中进行操作。我会试试这个,让你知道它是否适合我。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多