Middleware in ASP.NET Core - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to developer ramped up a channel dedicated to anybody who wishes to become a software developer or a better software developer in this video we'll continue talking about middleware in asp.net core so this is the second and the last video in a series in which we discussed this concept of middleware if you didn't watch the first video so middleware in asp.net core part 1 and you are really new to this concept of middleware in asp.net core then I strongly encourage you to pause this video and go and watch the first part so that you can get familiar with the concept of middleware you can find the link to that video in the description or even somewhere here on the screen however if you are a little bit familiar with middleware then you can also keep on watching this video because what we'll do now would be fairly understandable for you good now let's start with a short recap of what we did in the first video and the recap will be really short at the end we created two two custom middlewares and one was displaying hello from middleware one and the second one was displaying hello from middleware 2 in the browser what we have seen is that if we delete this a white dot next so if we forget it or if we would simply let's say comment it out right now what would happen that is that the middleware pipeline would be short-circuited and a request that comes in to this middleware when we perform this logic it won't be sent to the next middleware in the pipeline so a response will be generated and sent them back up the pipeline to the client that requested that specific resource so this next delegate is very important because it simply passes the request to the next middleware in the pipeline if we don't have it then the request is simply doesn't get any further now we have used it this F dot use delegate or method in order to come figure this custom logic it's a very basic logic but still custom logic on a specific request or in this case on the response because we write something on the response however if we want to use custom middleware we have also other methods or delegate and one of them is Epcot run and I would like to start with AB dot run because it is fairly similar with AB dot use in the scenario where we forget to add this next delegate so how can we use this middleware it's very simple and similar to AB dot use we'll use an async context here and we would need a placeholder for the HTML context because we want to write something to the HTML context and then we are ready to perform a certain action here so a weight context dot respawns dot write async and let's let's put it here also some tags the P tag because we want to have everything displayed on different lines and here we add hello from run let's make it like this hello from run middleware and that's basically it we added a third middleware a third custom middleware to the current mid-rare middleware pipeline so if we run this application right now let's see exactly what happens because we have three custom middleware that's right something as response and let's see what we will get on the browser and in a few seconds we should have the response we have hello from middleware one and hello from run middleware so what we notice is that we don't get this hello from middleware two which is here and the reason why this happens is that F dot run in a certain way is similar as B que veux-tu AB don't use when we forget to add the next delegate EPCOT Ron is considered to be a termination middleware so if you want to use it you would normally use it at the end of the pipeline so after this F dot run shouldn't be any other middleware there because what it would happen when the request will hit this middleware a response will be generated and sent back to the client so what happens with our request is that it comes in as we said in the previous video video from above it hits this use developer exception page then it it's sent to the next middleware and hello for middleware hello from middleware one is is written to the response then it is sent further because we have the next delegate to F dot run but F dot run is a termination middleware so we will write this here to the response context and then the response will be sent so this third middleware that we have configured will not be hit by the current request and let's prove that this is true let's simply cut this custom middleware from here and let's add it here after our second custom middleware so now we have only the use MVC middleware that wouldn't be hit but since we really don't want to do any MVC here we just want to show how middleware work we can then put it here and right now if we run this program once again the outcome should be slightly different and what I would expect is to have hello middleware one hello middleware two and hello from run middleware and in fact that's really what the case is so we have hello from middleware one hello from middleware two and finally hello from run middleware so in this case we see that all our custom middleware is is working properly and the app dot run which is always a terminating middleware also does its job and it writes something to the response and in the end we will have all this information written to the respawns so that is how AB dot run works the important thing to note here is that this F dot run is always a termination middleware and if you need to use it please use it as the last middleware in your middleware pipeline or use it in scenarios where for instance in certain conditions it is not important that the request should go further down the middle pipeline good so that's it from F dot run but now we have another very interesting method or delegate on the AI application builder instance that we have here which is app and it is called map and that's fairly cool so let's look a little bit deeper into what this app map means and how could it be useful and let's start by looking into the definition of this map method so if we press f12 we will get here on Microsoft asp.net call builder namespace and then we have map extensions so this map is an extension method in this case and we can see that it takes this I occasionally app which is typical for an extension method and this would be provided by asp.net core so what we would have to provide is a path string so this would indicate the path on which we want to perform some actions and we need to also provide an action of type I application builder so let's go back and try to do that first of all let's add the path string which would be angular let's say record there is some cool stuff about angular these days and then we have to provide this action and let's call this action and use a lambda okay so till here everything is okay we can call action dot run and here I just want to show you a really interesting thing I'll set this action as we saw in the definition is of type I application builder so it means that is the same type as this app and this means in turn that we can use action and call each of the extension methods that we can call on app like for example we used use or we used run so in this case we can call action dot run which would be exactly the same extension method as app run so it would take the exact same steps and the behavior would be identical so in Epcot run in this case let's take it let's take a sink then let's take the HDP context and use another lambda and let's here also await context dot response sorry let's go master back dot response dot right async and let's write here also something since this is about angular we will make it about angular but first of all let's add these P tags okay and let's say here angular 6 was released on May 3rd 2018 which is true you can check it out all the new features that are there for you in angular 6 good so now we are ready with this app dot map middleware let's however change here this F dot run because we haven't deleted it and let's call it non run nonmatter sorry middleware and let's run the software and analyze the outputs that we will see in the Windell let's give it a little bit of time so that everything loads and we will see then what we have on the screen so we have hello from is over 1 which is okay because we use that F dot use and also provided the next delegate so it will pass to the next one so that's why we have hello from middleware too but then we have hello from non map middleware which is the last middleware that we have configured right now in the pipeline and this happens of course because this was a get request to localhost five eight zero zero four so it will it didn't contain angular so this means internet that app dot map middleware was not hit so let's put in angular and you see that if we put your angular we get hello from middleware one from it aware to which is expected and then we get angular six was released on May 3rd and of course since we used app dot run the last Epcot run is not executed so this one that that we have here and then we don't have this message displayed so this is as expected good let's however try to do a different thing because we can use this app dot map to perform a real branching of the request so if an incoming request has this angular part then we can configure an entire branch for it and this means that we can configure an entire middleware pipeline just for this angular part and this would be fairly easy to do because what we could do here is simply add or change something let's change this into an app dot use here we have to provide a context and the next delegate ok and here let's a wait wait next that's fairly cool and now what we can do is however we can get go and do something else so let's for it for instance display here only angular 6 so let's take all this text out of here and let's then here use another middleware so it would be action dot use let's also add the semicolon and we make this a sink we need to provide the context and the next delegate and then use a simple lambda expression again and here let's just copy this from here has also stated in other developer ramp-up videos it's not ok to copy/paste code most of the times but for demo purposes and not to waste time writing a lot of text we copy and paste code that can be reused here and angular 6 let's say here was released and let's copy this entire or no let's don't copy it because right now we will change it a little bit and let's adhere action dot run and here we need an async context and the lambda ok semicolon at the end of course and here let's wait context dot risk response dot write async let's add the semicolon first so that you don't forget and let's add the P tag again so that's done so we have angular 6 was released and here on May 3rd 2018 and that's fairly cool and you will see it right now let's let's run it and see exactly what happens and analyze the response once again so when we make the call to localhost so to the content route let's say we get of course hello from Italy 1 hello from middleware to and from non map middleware because since we did since we did not use angular then of course we did not hit the app map middleware but then if we hit angular here you can see that the entire middle pipeline under app dot map was executed and we have angular 6 was released on May 3rd 2015 so this is how app dot map works you can use it to really branch requests based on a pot and then take that request through a totally different middleware pipeline then all the other requests and just as a simple example this might be useful for instance if you have in your project also something like a landing page and then maybe when requests for that landing page come in you maybe want to do some additional logging maybe you want to log some analytics and so on so in those cases it might be useful to use this app dot map to branch the entire request to a totally different middleware pipeline and in that case all other requests would use the normal let's call it default middleware pipeline while the request for your landing page which would be under this pad angular would be would run them through a totally different middleware pipeline from this point on at least good but besides this app dot map we also have a app dot map when and let's take a look into how app dot map when works so let's make a little bit of space here and use app dot map when and parentheses and semi columns of course once again first we have this squiggly line because we have to configure some things here and let's start once again by looking into the definition and this is very similar to what we have seen in app dot map this is an extension method it takes the IAP Legation builder instance and then what we have to provide here is not a path string but a function a so-called foam that will take in an HTTP context so the current HTTP context and return a bool so a true or false which we'll call predicate and then once again we need to provide an action so let's try to implement this here in to this app dot map when the first thing that we needed to provide was that funk that took in an FTP context and returned the bull so we have here context dot request and let's for instance in this case make an app dot map when middleware that would look at the query strings and if it finds in the query strings of a request URL a key which is called name then we will use it to perform some actions so we would have on the context request we have the query and here we have the contains key method let's use it and then we have of course to provide the key and let the key should be named so if we have difficult this context request query contains key then what we have to do is provide the action and we will do this exactly the same way we did also in map in F dot map so we'll call the action we'll find the action and use another lambda expression here and what we could do here is first of all VAR name equals and once again context K something went wrong so context oh of course I forgot here that we have first in this lambda of course we have X dot run and let's put a semicolon here and an async and this will take in a context and then another lambda expression and that would do is now we have the context so we can hear right var name equals context dot request dot query and here we can simply search for the key which should be a name so now we have this variable name and now we can await context dot response dot right async like we did previously let's also add here a semicolon and the P tags that we used throughout all these two videos and here we could write hello and let's use string interpolation here so it would be like this hello and then here name so it would take in our variable that we have defined previously so that should do it let's run the application and see if everything adds up and if the outputs are expected so once again the first request is a get request for the base URL so we won't hit any of this map or map when middlewares but if here for instance let's add a query string which would be named sorry we have to add question mark name equals dan and let's see what's happened and you see we have hello Dan here which is kind of cool so in this case what happened is that we use that query string and when the request started to go through this middleware pipeline it hit this app map when and this condition was true so then we executed the action which is an action dot run and there we wrote something to the response now I won't dwell any further on this but the main idea is that you can use both the app map that we have used here and the app map when methods to branch requests because also here we could define an entire middleware pipeline that would be branched from the let's say parent pipeline and the request would go through all the middleware that we could configure here so it's exactly the same as it works also in app dot map then the question would be of course when to use app dot map and when to use app dot map when and the answer here is fairly simple we have seen that app map we can do branching only based on the incoming path while when we use app map when we can do branching based on any condition they could that we could infer in a certain way from the HTTP context and if we take a look at the HTTP context we would see that we have really a lot of information available and we could do here or apply logic based on headers based on if it is HTTP based based on payload so really based on any property that is there on the HTTP context and from which we could return a true and a false we can apply some custom logic so once again in Epcot map everything is very simple but we can do this branching only based on the incoming request part while when we use app map when we came to bridge we can do branching based on any property that we find on the HTTP context which is fairly powerful I would say so that's pretty much it on middleware in asp.net core in part 1 and part 2 we covered the fundamental topics on what a middleware is how it is supposed to work how we can configure middleware pipeline in asp.net core and so on and we also have configured our own very small custom middleware where we have applied some custom logic in this case it was very simple just writing something to the response but we saw that if we use F dot use and we forget to add the next delegate then this will short-circuit the entire middleware pipeline then we have checked F dot run and we saw that this is a termination middleware so nothing that comes afterwards or none of the middleware that come afterwards won't be will be executed and then we we took a look into this app dot map and into this F dot map when methods which allow us to branch request and put them through a totally different pipeline based on a request part or on any property that we can find in the HTTP context if you find this content useful please subscribe to develop a ramp-up feel free to share this content with your friends and your network and should you have any feedback or question just hit me with a comment also don't forget to thumbs up thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 4,885
Rating: 4.9483871 out of 5
Keywords: ASP.Net Core, DotNetCore, web development, software development, programming, C#
Id: A1ZmMoiBELc
Channel Id: undefined
Length: 25min 49sec (1549 seconds)
Published: Fri May 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.