Join Jeff Fritz for an in-depth discussion in this video Map middleware, part of ASP.NET Core: Middleware.
- [Instructor] Now the last type of middleware that we talked about uses a keyword of map and if we look at the map keyword, there's actually two methods that you can call map to match a path that was requested, so if a path is requested/stuff, you can have it go do some stuff that's appropriate to whatever that is. You can do something like this.
And now I have the configuration, the action that we want it to actually take on that. So we can do something like... And I'll pass into async context and I'll do something similar here, context response, content type, text html, and content response write async.
Here is your stuff. So now I'm mapping a request when /stuff, when that path is requested, we're going to go execute these lines on 50 and 51 and then terminate. There is no next call. Let's see how this runs. I'll dotnet run. My application is running and I'll come back here. If I request my index page again, that returns normally and if I click on it, I get my hosting environment name.
Okay, that's fine. If I go to the root of my application I get my hello world. Click on that I get my hosting environment name and you can see at the footer there with the development environment. And if I browse to /stuff, here is your stuff, and I get the output timing and my hosting environment name. So now I've mapped a request because I browsed to a different location. Okay, so here's a way for us to set up different simple locations so that we can make a very simple and streamlined application that responds very, very quickly all in one file here.
I didn't write a bunch of extra things here. I can process requests and output them right here or I can put my logic out in additional methods, out in additional classes like environment middleware as I demonstrated here on line 33 and have it do other things. But that map was only on the request path. There are other things that we can do with our map with the map when command.
Let's take a quick look at that. So map when takes in a func of http context and returns a bool, it returns a Boolean, so we're going to do some sort of a test on our context. Right, so I might do something in here. You know what, I don't even need an inner anonymous method like that. I can say something like context.request headers user agent contains Apple iPhone and then I can tell it to use some sort of an action of type of application builder and maybe that's iPhone route, right? And generate that method for me.
So now down at the bottom here I have a method called iPhone route that takes in an I application builder. Let's rename that to app. And we can do something when our user agent contains Apple iPhone so if there's something specific that you want to output for a different user agent or maybe you want to do something specific if a request comes from outside the IP addresses that you're working with or maybe there's a cookie that you want to check to see if it exists and behave appropriately when that cookie is present, you can use the map when to change the request location, to change the behavior of the pipeline because that specific situation, that condition exists inside of the request.
Let's go back to the slides and let's get a little bit more information now that we know a little bit more about how to use run, map and use to build middleware, order our middleware, and structure an application very, very easily inside of our startup configure method.