WEBVTT

00:00:01.743 --> 00:00:04.409
There are several ways to add headers to

00:00:04.409 --> 00:00:06.743
responses of your ASP.NET application.

00:00:06.743 --> 00:00:09.470
Which one you choose is up to you and your

00:00:09.470 --> 00:00:12.076
team. There's no way that is better than

00:00:12.076 --> 00:00:14.743
the other, just pick one that appeals to

00:00:14.743 --> 00:00:17.742
you the most. You could write a filter and

00:00:17.742 --> 00:00:21.076
the code is exactly the same for both

00:00:21.076 --> 00:00:25.743
ASP.NET and ASP.NET Core. Here's on I did

00:00:25.743 --> 00:00:28.243
in ASP.NET. A filter is an attribute you

00:00:28.243 --> 00:00:31.743
can put above a controller or an action.

00:00:31.743 --> 00:00:34.493
There are several types of filters, but an

00:00:34.493 --> 00:00:36.743
ActionFilter is great to add headers

00:00:36.743 --> 00:00:39.854
because it lets you manipulate both the

00:00:39.854 --> 00:00:42.409
incoming request as the outgoing response,

00:00:42.409 --> 00:00:46.314
and the latter is the one we need. A class

00:00:46.314 --> 00:00:50.142
derives from a base class in a framework

00:00:50.142 --> 00:00:52.314
called ActionFilterAttribute. The method

00:00:52.314 --> 00:00:56.142
to override to add headers to the response

00:00:56.142 --> 00:00:59.243
is OnResultExecuting. The provided

00:00:59.243 --> 00:01:01.743
ResultExecutingContext object contains the

00:01:01.743 --> 00:01:04.242
action result returned from the

00:01:04.242 --> 00:01:07.243
controller. Only when this is a view

00:01:07.243 --> 00:01:09.743
result we want to do something.

00:01:09.743 --> 00:01:12.409
ResultExecutingContext object we got also

00:01:12.409 --> 00:01:16.076
provides access to the HttpContext. From

00:01:16.076 --> 00:01:18.742
there, we can get to the Response object

00:01:18.743 --> 00:01:21.342
on which we can add a header. To

00:01:21.342 --> 00:01:24.942
demonstrate how everything works, it's a

00:01:24.942 --> 00:01:29.142
test header HeaderType with a value of

00:01:29.142 --> 00:01:30.993
HeaderValue. In the controller, I can

00:01:30.993 --> 00:01:33.457
apply the filter by putting the attribute

00:01:33.457 --> 00:01:36.965
above the class. With C# attributes, you

00:01:36.965 --> 00:01:40.342
can omit the attribute suffix. You can

00:01:40.342 --> 00:01:43.243
also put it above one or more actions in

00:01:43.243 --> 00:01:46.243
the controller if you want finer-grained

00:01:46.243 --> 00:01:47.743
control. When I run the application now

00:01:47.743 --> 00:01:51.143
and use the F12 tools of the browser, you

00:01:51.143 --> 00:01:54.343
can see the header is sent with the

00:01:54.343 --> 00:01:57.368
response. It has no effect on the browser

00:01:57.368 --> 00:02:00.743
whatsoever, it is just being sent along.

00:02:00.743 --> 00:02:02.743
From the next module on, we'll see headers

00:02:02.743 --> 00:02:05.409
that have an effect on the browser and are

00:02:05.409 --> 00:02:08.043
useful for the protection of our app. You

00:02:08.043 --> 00:02:10.443
can also register the filter as a global

00:02:10.443 --> 00:02:13.743
filter so that it is automatically applied

00:02:13.743 --> 00:02:16.187
to all controllers without the need for

00:02:16.187 --> 00:02:19.298
you to put the attribute above every

00:02:19.298 --> 00:02:21.993
controller. In ASP.NET, there is a file

00:02:21.993 --> 00:02:23.600
called FilterConfig.cs under App_Start

00:02:23.600 --> 00:02:26.493
where you can do that. In ASP.NET Core,

00:02:26.493 --> 00:02:28.600
you can do it in the ConfigureServices

00:02:28.600 --> 00:02:32.993
method where you add MVC to the service

00:02:32.993 --> 00:02:35.409
collection. Next, we'll look at another

00:02:35.409 --> 00:02:41.743
way to add HTTP headers with middleware.
