From the course: Advanced ASP.NET Web API 2.2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Adding a custom response header

Adding a custom response header

From the course: Advanced ASP.NET Web API 2.2

Start my 1-month free trial

Adding a custom response header

- [Man] In Web API we can create custom handlers to add custom headers to our responses. So, let us go to Visual Studio and see this in action. In Visual Studio go to Solution Explorer and then add a new class in our Custom Handlers folder. So add Class. We are going to name our handler Custom Header Handler and then we need to inherit from the base class Delegating Handler. So (typing) Delegating Handler. Let us import the necessary name space which is the System.Net.http. And here we are going to override the method that we need which is the SentAssync method. Now in here let us remove the return so we can work with the response. For that we write var response is equal to base.SendAssync. And now, if you want to add custom headers to this response, you need to write response. and then scroll down in here to Result.Headers.Add method. So the first bar meter is going to be the name of the header and the second bar…

Contents