From the course: ASP.NET Core: Middleware

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Adding an extension method

Adding an extension method - ASP.NET Core Tutorial

From the course: ASP.NET Core: Middleware

Adding an extension method

- [Instructor] We could further improve our implementation so far, well, arguably improve it, by adding an extension method. The current implementation means that program.cs needs to know about the custom middleware class. We could try to decouple this a little bit further because the custom class is in the same namespace as program.cs, and if the custom class exposed an extension method, then program.cs would only need to run the extension method, but would not need to know the specific name or any other details about that class. It's a minor thing, but we could consider it. And it basically means that we have to add a public static method. The extension method will be part of IApplicationBuilder so that we can later call app., say, UseXYZ, and in that UseXYZ method, we do the registration. So just a subtle change, but I still think it might be worth it. So let's do it. We could create a separate class file for that extension method, but I like putting it in the same file where I…

Contents