Translation ASP. NET Core 2.0 Middleware

Source: Internet
Author: User
Tags httpcontext terminates

Problem

How do I create one of the simplest ASP. NET Core middleware?

Answer

Using VS to create an empty project with ASP. Startup.cs, note the Configure () method in the. NET Core 2.0:

 Public void Configure (Iapplicationbuilder app, Ihostingenvironment env) {    app. Run (async (context) =    {        await context. Response.writeasync ("Hello world! (Run) " );    });}

The better way to create a request pipeline is to use the extension method on Iapplicationbuilder:

 Public Static void Runhelloworld ( this iapplicationbuilder app) {    app. Run (async (context) =    {        await context. Response.writeasync ("Hello world! (Run) " );    });}
 Public void Configure (Iapplicationbuilder app, Ihostingenvironment env) {    app. Runhelloworld ();}

Run, this page displays:

Above we use Iapplicationbuilder.run () to configure the middleware, the other way is Iapplicationbuilder.use ():

 Public Static void Usehelloworld ( this iapplicationbuilder app) {    app. Use (async (context, next) =    {        await context. Response.writeasync ("Hello world! (use) \ n")        ; await next ();    });}
 Public void Configure (Iapplicationbuilder app, Ihostingenvironment env) {    app. Usehelloworld ();    App. Runhelloworld ();}

Run, this page displays:

It is a better practice to use middleware as a separate class definition:

 Public classhelloworldmiddleware{Private ReadOnlyrequestdelegate _next;  PublicHelloworldmiddleware (requestdelegate next) {_next=Next; }     Public AsyncTask Invoke (HttpContext context) {awaitContext. Response.writeasync ("Hello world! (use in Class) \ n"); await_next (context); }} Public Static classusehelloworldinclassextensions{ Public StaticIapplicationbuilder Usehelloworldinclass ( ThisIapplicationbuilder app) {        returnApp. Usemiddleware(); }}
 Public void Configure (Iapplicationbuilder app, Ihostingenvironment env) {    app. Usehelloworld ();    App. Usehelloworldinclass ();    App. Runhelloworld ();}

Run, this page displays:

Discuss

Middleware is a component that intercepts HTTP request and response messages. We create a request pipeline for our application by creating these component chains.

We create this request pipeline with the Iapplicationbuilder parameter of the Configure () method, and the Iapplicationbuilder parameter has the following method:

    • Run (): Adds the middleware and terminates the request pipeline (that is, no longer calls the next middleware).
    • Use (): Add a middleware, using a lambda expression, or a specific class.
    • Map (): Add middleware based on the request path.
Run

This method accepts the Requestdelegate delegate as a parameter and accepts the HttpContext parameter when the delegate method is called. This delegate method returns void because it terminates the request pipeline.

Use

This method takes the Func delegate as a parameter, and this delegate method has two parameters, namely HttpContext and next, which points to the next middleware, which returns Null (Task). If the next middleware is not called, the request pipeline is terminated (as with the run effect).

Usermiddleware

When you create a middleware from a separate class, we use the Usemiddleware method and the concrete implementation type as the generic parameter.

In the middleware class, there are two parts that are important:

1. The constructor accepts Requestdelegate. When this delegate is called, the current request is passed into the next middleware.

2. It has an invoke method that receives the HttpContext parameter and returns an empty (Task). When middleware is needed, the framework invokes this method proactively.

Note: Implementing middleware in a separate class and encapsulating it with Usemiddleware is a best practice.

Extension methods

Note that the extension method differs, RUNXXX does not return a value, and Usexxx returns a value (Iapplicationbuilder). This is because run () terminates the request pipeline, and use () may be linked to other middleware.

Order

The middleware is called sequentially in the order in which they appear in the Configure () method. The response back to the client also goes through the same middleware pipeline.

Source code Download

Original: Https://tahirnaushad.com/2017/08/14/asp-net-core-middleware/

Translation ASP. NET Core 2.0 Middleware

Related Article

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.