locked
Add custom response header RRS feed

  • Question

  • User1587609066 posted

    Hi,

    I want add a custom response header with all.

    I add in the Configure Method in Startup.cs

    app.Use(async (context, next) => { context.Response.Headers.Append("my-header", "*"); await next(); });

     Why this has no effect?  How to do it?

    Thanks!

    Friday, August 28, 2015 11:44 AM

Answers

  • User-1273603057 posted

    The code below produces the following Web browser results:

    Server=Microsoft-IIS/10.0
    my-header=*
    Hello World!

    using System.Linq;
    using Microsoft.AspNet.Builder;
    using Microsoft.AspNet.Http;
    using Microsoft.Framework.DependencyInjection;
    
    namespace WebAppLight
    {
        public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
            }
    
            private const string CR = "<BR>";
    
            public void Configure(IApplicationBuilder app)
            {
                app.Use(async (context, next) => 
                {
                    context.Response.Headers.Append("my-header", "*");
                    await next();
                });
    
                app.Run(async (context) =>
                {
                    var headerString = string.Join(CR,
                        context.Response.Headers
                            .Select(r => r.Key + "=" + r.Value.First()));
    
                    await context.Response.WriteAsync(headerString+CR);
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 29, 2015 2:41 AM

All replies

  • User-1084200161 posted

    It works for me, using your code in Beta 6.  One thing to remember is to place this at the right spot... since you said you wanted this for everything, you'd probably put it towards the top of Configure method.  For example, if using MVC, you'd want this above app.UseMvc() line..

    Friday, August 28, 2015 9:12 PM
  • User-1273603057 posted

    The code below produces the following Web browser results:

    Server=Microsoft-IIS/10.0
    my-header=*
    Hello World!

    using System.Linq;
    using Microsoft.AspNet.Builder;
    using Microsoft.AspNet.Http;
    using Microsoft.Framework.DependencyInjection;
    
    namespace WebAppLight
    {
        public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
            }
    
            private const string CR = "<BR>";
    
            public void Configure(IApplicationBuilder app)
            {
                app.Use(async (context, next) => 
                {
                    context.Response.Headers.Append("my-header", "*");
                    await next();
                });
    
                app.Run(async (context) =>
                {
                    var headerString = string.Join(CR,
                        context.Response.Headers
                            .Select(r => r.Key + "=" + r.Value.First()));
    
                    await context.Response.WriteAsync(headerString+CR);
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 29, 2015 2:41 AM