From the course: ASP.NET Core: Middleware

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Implementing middleware in an external class

Implementing middleware in an external class - ASP.NET Core Tutorial

From the course: ASP.NET Core: Middleware

Implementing middleware in an external class

- [Instructor] I'm going to return to Visual Studio, and now we can actually, besides using the use statement here or a run statement here and providing an in-line function, we can actually move our middleware out to a class. So I'm going to do that here by adding a piece of middleware, and we do that by using a statement, app use middleware. Now, you can either use the non-generic type here and pass in a type specifically here, but I like to use the generic type, because I think it's a little bit more readable. So I will create a class called environment middleware. And if I tell it to generate that class in a new file, Visual Studio creates this class for me. It's an internal class called environment middleware. Internal's fine, that's a fine scope for this application. Now when this class is created for me by the HTTP pipeline, at initialization time, it's going to pass into it the next delegate. So let me create a constructor using ctor snippet, and I'll come up here and I will…

Contents