Part 6. ASP.NET Core Middleware | Request Delegate | How to use RUN, USE MAP, USEWhen, and MAPWhen.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends, welcome to programming concepts.  My name is Amit and this is part 6 of ASP.NET   Core MVC tutorial. In this video we will talk  about a very simple yet powerful concept in core,   that is Middleware. This is continuation  to part 5 ASP.NET CORE Main method,   so please watch it before proceeding to this  one, I shared the link in the description.   So middleware is a very simple concept, whenever  end user access your application an http request   has been generated and this request reached  to developers in the form of http context.   right! So that developers can get the required  information and play with that information,   so in simple term this phenomena where you  decide what to do with http request is known as   middleware then depending on your middleware  configuration request will go to your   application code we'll discuss why I add the word  depending on your middleware configuration later.   Then response will go back to your middleware  and finally it will reach to end user in that way   middleware have control to both  your requests and responses.   Before moving forward let's go  through with the definition first   so middleware is a software that's assembled into  an app pipeline to handle requests and responses   so the word pipeline is important here, in  application, there will be multiple middleware   components so you can think of components as a  small function who are doing specific kind of   tasks obviously here with respect to requests  and responses. Each component chooses whether   to pass the request to the next component  in the pipeline and can perform work before   and after the next component in the pipeline.  We'll discuss about this scenario practically.   Next is, Request Delegates are used to build the  request pipeline. The request delegates handle   each HTTP request. What does that mean? That means  Microsoft already provided you the functionality   in the form of request delegates so that you  can use them to build the request pipeline   as you can see the request delegate handle each  HTTP request so we only need to learn how to use   this request delegate. If this is confusing at  the moment do not worry in next 10 to 12 minutes   everything will be crystal clear. Let's see how to  implement request delegates. So request delegates   can be implemented via three different extension  methods so if we have to implement request   delegates or you can say middleware components  then we already have three extension methods   available first is Run second is USE third is  MAP and for MAP we have a subset called MAPWHEN   and for USE we have a subset called USEWHEN. So  that's enough theory let's take that in action let's go to visual studio and this is  the project which we are working with   let's open startup.cs class so within this class  you can see there is a method called configure   so if you can see in the comment this  method gets called by runtime use this   method to configure the HTTP request pipeline  so let's delete the code within configure method   and this method accepts two parameters  iapplicationbuilder and iwebhostenvironment   so to make it simple let's delete  iwebhostenvironment as well. so if i go back to the slides you can see  there are three extension methods available   to implement request delegates  and the first one is run   let's get back to visual studio so  to implement run extension method   we will use the object of i application builder  so app.run bracket open and in the description   you can see this is an extension method which  accepts parameter of type request delegates   and in the definition you can see that it adds a  terminal middleware delegate to the application's   request pipeline means whatever code written  beyond this middleware will never be executed   just like we have a return command within a  function or method nothing will be executed   beyond return statement, similarly nothing will  be executed when terminal middleware hit. simple   just to add sometime it is also referred as a  short circuiting because if you go to microsoft   website in their documentation this word has  been used for explaining terminal middlewares   nothing specific so if anybody asks what is short  circuiting in middleware pipeline it simply means   addition of terminal middleware in the application  pipeline where it's short circuit and doesn't   allow further middlewares to execute okay let's  move forward so app.run async space context such   that curly brace open, curly brace close bracket closed semicolon. so within those curly   braces how you can decide what this middleware  should do so okay you got the context object and   within this context object you have necessary  information about the object for now let's say   we only wanted to write something in response  so await context dot response dot write async let's say called from run middleware  and that's it so let's run this program and as expected it will print the  message called from run middleware next let's prove that this run method acts as  a terminal middleware and it doesn't allow us   to go beyond its execution point and to do  this let's copy and paste this run method   and let's change the message and  say called from run middleware 2   just add slash n to separate response  from middleware 1 and middleware 2. and let's run this program and as you can see only   first middleware executed as the messages called  from run middleware so that simply means that run   method terminates the execution and doesn't allow  us to go beyond its execution point hope this   clear what is the purpose of run extension method  let's move to our slide and the next extension   method is use so similar to run we have out of  the box use extension method available so app.use   in the definition you can see a function that  handles the request and a calls the next function so app.use bracket open async bracket open  context comma next bracket close such that curly   brace open curly braces closed bracket close  semicolon so again within both the curly braces   here you will get your context object  along with the pointer to the next   delegate in the pipeline and here also you can  decide what to do with this http request object   so again for simplicity let's copy and  paste the await line from run method   and let's change the message called from use  middleware slash n so let's run this program ideally it should print the  message from run middleware as well   as use is not a terminal middleware but as you can  see it only prints a message from use middleware   and the reason is that we didn't use the next  method and if we don't use the next then use will   also acts as a terminal middleware so let's use  the next method and to use it write await space   next dot invoke bracket open bracket close  semicolon so let's run the program and you   will see the result as expected so it will print  a message from use as well as from run middleware next let's see the sequence of  execution in case of next middleware so just write it down write async let's  say message will be one space slash n to   separate the response and when the  next invoke method will be called   then control will go to next middleware and  let's say message will be 2 then again the   next invoke method will be called  and then it will go to run method   and here let's print three once the  execution completed from run method   then it will go back to use let's say 4 and once  the execution of this use method is completed   then it will get back to first use method let's  say print 5. so let's execute this program and as you can see the response is  as expected one two three four five so nothing complicated invoke  is also a method just like in c   sharp when we called a method so that method  will be executed first then the response get   back to the main method similarly here as  well so once we call the invoke method the   next method will gets executed then the  response came back to the main method simple so the next extension method is use when so  let's type app.use when bracket open and in   the definition you can see conditionally  creates a branch in the request pipeline   that is rejoined to the main pipeline so the  word rejoined here is important this branch   is rejoined to the main pipeline if it doesn't  short circuit or contains a terminal middleware   all right so just like if statement if  condition matches then this component will   gets executed else it will be ignored so let's  implement that app.use when bracket open context   such that let's say we want to match the  query string so context dot request dot   query dot contains key bracket open  let's say the query string will be branch and in next you will get the i application builder  so let's name it my conditional app builder such that curly brace is open curly braces close  bracket close semicolon and within this let's type   my conditional app builder dot use you can use  run as well bracket open async context next   such that curly braces open curly braces  close bracket close semicolon so here also   within both the curly braces you will  get the context object to play with   and instead of app you have the same reference  variable in the form of my conditional app builder   so you can name it anything so so let's copy  and paste the content from use middleware   and let's change the message from  1 to 11 and let's say 5 to 55. so let's run this program   and in the output you can see that 11 and  55 is not available that simply because we   haven't added a query string yet so let's say  slash amit question mark branch is equal to   10 and then hit enter and in the output at the  top and bottom you can say 11 and 55 is printed   all right so that's it for use and useWhen  and the next extension method to implement   middleware component is map so let's type app.map  bracket open and you can see in the definition   branch is the request pipeline based on matches  of the given request path if request paths start   with given path the branch is executed so if url  matches then this middleware component will gets   executed so the first parameter is path string  let's say we want to execute when we get   map1 as a path next here also we will get the i  application builder just like use when let's name   it map app builder once we have i application  builder object we can control the http request   correct so mapappbuilder dot run bracket open  async context such that copy and paste the await   command and let's change the message  to map one so let's run this program and because we use the run middleware within  map so it will going to terminate but you are   still saying that one two three four five as an  output that's simply because we haven't changed   the url so let's name it map one let's enter  and you will see the output as expected map one another very important aspect about map is that  it supports nesting means map within map or you   can say branch within branch is possible here  for example let's create in the map app.map   and let's name the path as main map path and  let's name the i application builder as main app   such that and for this main map path let's  use the use command so main app dot use async   and let's print the message as main and to invoke  next method let's write await next dot invoke   that's it let's say within main   map path we also have another branch  so main app.map slash nested map path nested app such that nested app.use and  let's copy and paste the use command   and change the message from main to nested alright so what it will do it will first map with  main map path and if it matches then it will print   main then within minimap path if it is matches  the nested map path then it will print nested   so let's run this program and in the url we need to write slash main map path or when i hit  enter it will print main and if i write it down slash nested map path and hit enter then it will print main and nested all right next  very important aspect is that instead of writing   path within path map can also match multiple  segments at once okay so let's take that example   so app.map and let's name the branch  will be slash map one slash segment one   and let's name the i application builder as  handle multi-segment app let's use the run command   and print the message as map multiple segments let's run the program let's let me copy the branch branch  path slash map one slash seg1 and   when i replace it in the url then you can  see the response is map multiple segments all right so the last one which we  need to cover is map1 so let's type   app.mapWhen drag it open so in the definition  you can see map and branches the request   pipeline based on result of the given predicate  any predicate of type func http comma bool   can be used to map requests to a new branch of  the pipeline let's see that let's say we want to   execute this mapping command when the query string  contains branch so let's say context such that context.request.query.Contains key branch and we will  get the i application builder let's name it   handle branch app such that and let's  use the run command here now copy and   paste that and let's change the message to  branch map then let's close the curly braces let's execute the code initially you will see one two three four five  as an output so let's add the query string branch   let's say equal to 10 and let's re-execute  and you will get the message branch map When   all right so let's get back to the slide so  we have successfully implemented run map use   MapWhen and use them, so finally let's see  how we can implement the custom delegates sorry not the custom delegates it's called custom  middleware so i guess this video is quite lengthy   so we will cover custom  middleware in our upcoming video  so if you have any query do ask me on  comment till then thanks for watching
Info
Channel: PRO Concepts
Views: 5,078
Rating: undefined out of 5
Keywords: what is middleware in asp.net core, middleware components in asp.net core, asp.net core middleware explained, asp.net core middleware fundamentals, asp.net core middleware basics, asp.net core middleware pipeline explained, asp.net core middleware pattern, asp.net core middleware tutorial, understanding asp.net core middleware, asp.net core request processing pipeline, asp.net core request pipeline, asp.net core create processing pipeline, ProConcepts, amit, ProgrammingConcepts
Id: tPMsdA8lOcg
Channel Id: undefined
Length: 20min 36sec (1236 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.