Join Jeff Fritz for an in-depth discussion in this video Ordering middleware, part of ASP.NET Core: Middleware.
- [Instructor] The request for that environment middleware is down here after the static files were already processed and terminated on line 33. Remember, requests in the pipeline are processed for top to bottom so because that static file request terminated on line 33, it doesn't actually proceed down and put the environment middleware information into the request on line 47. So there's a simple solution to this.
Let's reorganize the pipeline so that our environment middleware is triggered before the static files. So I'm going to use the hot key of alt + up and move my middleware where I want it to be. I can use the alt + down keyword and move it down. But I'm going to place it right there on line 33 and bump static files down to 34. I'll save that. And restart my application.
My application is running and now I'll request my index page again and there you go. I still don't have that paragraph at the bottom, but if I click on this I do now get my hosting environment name is development. Why is that? Why can't I get that footer on the bottom of that static content? Going back over here, let's actually set a break point inside of my middleware here and I'll set it right here after next is called.
This time I'm going to start my application using Visual Studio and we'll see what happens. Alright, so my application is running. I'll go back over to my browser. I'll request my index page again. And I'm bumped back here into Visual Studio and I'm at this debug point here. If I look at the content type that's being returned, there is something there and response content type contains html, so let's step through and it's going to try and write in from that information.
And it's showing me here in my log, the file index.html was not modified. And the request was completed. Let me run back over to Firefox and I don't see it on the page. Let's actually look at the source code here. And it's not being output. This is because the static files type doesn't allow us to modify the files that are being output. We can't add things on to the end of this static request.
We can't modify that response cache because the response has already been flushed. So back here in Visual Studio, I'm going to stop the debugger. I'm going to turn off this break point. And I want to make sure that we remember that in our startup method here we usually don't end with this app.Run statement. We usually see our applications end with app.UseMvc here and I don't have the MVC extensions loaded because MVC isn't part of this project, but we usually see that this is where our requests end and any request that's emitting generated html will have our paragraph added to the bottom of the page.