Add custom header to all responses in Web API

后端 未结 8 1084
我寻月下人不归
我寻月下人不归 2020-12-07 18:30

Simple question, and I am sure it has a simple answer but I can\'t find it.

I am using WebAPI and I would like to send back a custom header to all responses (server

8条回答
  •  太阳男子
    2020-12-07 19:21

    I had the same problem while trying to add a new header to the whole controller, just add "services.AddHttpContextAccessor();" to startup.cs then create your controller

    public class EnController : Controller{
    
            public EnController(IHttpContextAccessor myHttpAccessor)
            {
    
                myHttpAccessor.HttpContext.Response.Headers.Add("Content-Language", "en-US");
    
            }
    
           ... more methods here... 
    
    }

提交回复
热议问题