Dotnetcore Add middleware run,use Map mapthen four extension methods

Source: Internet
Author: User

What is middleware

Middleware is a component that processes request requests and responses responses in a pipeline, and each component can choose whether to have the request go into the next component to be processed.

The translation is not good, we can see the original middleware

In more detail, you can also refer to the works of the Great God in the garden;

There's Uncle Tom's interpretation. asp 5 & MVC6 Series (6): Middleware detailed

Artech the Great god's ASP. [1]: What is a middleware?

How to create a middleware please refer to the English document middleware or

Linezero's ASP. NET Core Development-middleware (middleware)

To properly use middleware to build your own program, you need to understand how the Run,use,map,mapthen four methods are used, the following Ricman will own understanding and share with you.

First, Run extension method

The Run method is in the description: Add a middleware at the end of the pipeline; it is the last middleware to execute. That is, it will not execute the next middleware after execution. The following code example.

 //This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); varLoger = Loggerfactory.createlogger ("Testlogger"); //The first run executes theApp. Run (AsyncContext ={Loger. Loginformation ("Run 1 start"); awaitContext. Response.writeasync ("Hello World!,run 1"); Loger. Loginformation ("Run 1 End");            }); //the second run did not executeApp. Run (AsyncContext ={Loger. Loginformation ("Run 2 start"); awaitContext. Response.writeasync ("Hello World!,run 2"); Loger. Loginformation ("Run 2 End");                    }); }

The result of the output is:

Only the contents of the first run are printed. The program does not respond to the contents of the second run method.

Second, use extension method

The use method is to add a middleware to the pipeline. If you call next. The Invoke () method, which executes the next middleware. Let's make a slight change to the example above:

 //This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); varLoger = Loggerfactory.createlogger ("Testlogger"); //the Use method executes theApp. Use (Async(context,next) ={Loger. Loginformation ("Use 1 start"); awaitContext. Response.writeasync ("Hello World!,use 1"); Loger. Loginformation ("Use 1 End");            }); //The Run method does not performApp. Run (AsyncContext ={Loger. Loginformation ("Run 1 start"); awaitContext. Response.writeasync ("Hello World!,run 1"); Loger. Loginformation ("Run 1 End");                    }); }

What is the output of the result?

There is no call to next. Invoke (); The middleware of the tail end is not executed within the Run method. Using the use method instead of calling next. Invoke (), the effect of use is consistent with the effect of run. To verify the effect of use, we'll modify the code.

//This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); varLoger = Loggerfactory.createlogger ("Testlogger"); //performed aApp. Use (Async(context,next) ={Loger. Loginformation ("Use 1 start"); awaitContext. Response.writeasync ("Hello World!,use"); awaitNext.                Invoke (); Loger. Loginformation ("Use 1 End");            }); //no execution.App. Run (AsyncContext ={Loger. Loginformation ("Run 1 start"); awaitContext. Response.writeasync ("Hello World!,run"); Loger. Loginformation ("Run 1 End");                    }); }

At this point, enter the following results

The use and run code snippets are executed. It is important to note that multiple middleware can be added to the pipeline, they are executed sequentially, and the order of execution is consistent with the order of the code in the Configure method.

Third, map and Mapthen

The map compares differently, it adds middleware to the pipeline, it adds branches to the pipeline. The branching of the pipe is increased by means of mapping the path. We keep the above example and add the code. As follows:

//This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); varLoger = Loggerfactory.createlogger ("Testlogger"); //performed aApp. Use (Async(context,next) ={Loger. Loginformation ("Use 1 start"); awaitContext. Response.writeasync ("Hello World!,use"); awaitNext.                Invoke (); Loger. Loginformation ("Use 1 End");            }); App. Map ("/maptest", Handlemap); //no execution.App. Run (AsyncContext ={Loger. Loginformation ("Run 1 start"); awaitContext. Response.writeasync ("Hello World!,run"); Loger. Loginformation ("Run 1 End");                    }); }        Private Static voidHandlemap (Iapplicationbuilder app) {app. Run (AsyncContext =            {                awaitContext. Response.writeasync ("Hello, that's Handle Map");        }); }

To run, we entered "http://localhost:12716/mapTest" in the browser to get the following results:

The Maptest branch was carried out.

Mapthen is more interesting, literally feeling a bit like the meaning of a query. That's right. It is to handle the request that meets the criteria to execute the given method. We modify the code

//This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); varLoger = Loggerfactory.createlogger ("Testlogger"); //performed aApp. Use (Async(context,next) ={Loger. Loginformation ("Use 1 start"); awaitContext. Response.writeasync ("Hello World!,use"); awaitNext.                Invoke (); Loger. Loginformation ("Use 1 End");            }); App. Mapwhen (Context=           {              returnContext. Request.Query.ContainsKey ("Q");            }, Handlequery); //no execution.App. Run (AsyncContext ={Loger. Loginformation ("Run 1 start"); awaitContext. Response.writeasync ("Hello World!,run"); Loger. Loginformation ("Run 1 End");                    }); }         Private Static voidHandlequery (Iapplicationbuilder app) {app. Run (AsyncContext =            {                   awaitContext. Response.writeasync ("Hello, this is Handle Query");        }); }

What we have to deal with is that if there are parameters in the URL that contain the Q-letter, then the Handlequery method is executed. Look at the results.

As can be seen, Mapwhen can handle a lot of things, such as we have to deal with the request header of a specific content, you can use the Mapwhen to handle.

If you think this article is helpful to you, please click "Recommend"

Dotnetcore Add middleware run,use Map mapthen four extension methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.