ASP. NET Core Middleware Setup Tutorial (7) _ Practical Tips

Source: Internet
Author: User
This article is mainly for you to introduce the ASP. NET Core middleware Setup tutorial, with a certain reference value, interested in small partners can refer to

ASP. NET core-Middleware

In this chapter, we will learn how to set up middleware. Middleware technology controls how our applications respond to HTTP requests in ASP. It can also control application exception errors, which is a key to how authentication and authorization users perform specific operations.

    • Middleware is a software component that is assembled into an application pipeline to process requests and responses.

    • Each component can choose whether to pass the request to the next component in the pipeline, and can perform tasks before and after certain operations in the pipeline.

    • The request delegate is used to build the demand pipeline. The request delegate is used to process each HTTP request.

    • Each middleware is an object in the ASP. NET core, and each piece has a very specific, focused, and limited role.

    • Ultimately, we need a lot of middleware to provide the appropriate behavior for the application.

Now let's assume that we want to log every request to our application.

    • In this case, the first piece of middleware we install into the application is a log component.

    • This log program can see all incoming requests, then the middleware just records some information and then passes the request to the next block middleware.

    • Middleware is a series of components that appear in this processing pipeline.

    • The next middleware we install into the application is an authorization component.

    • A component may be a token that looks for a specific cookie or accesses an HTTP header.

    • If the authorization component discovers a token, it allows the request to continue.

    • If not, the authorization component itself may respond to the requested HTTP error code or redirect the user to the login page.

    • Otherwise, the authorization component passes the request to the middleware of the next router.

    • A router looks at the URL and determines the next action.

    • The router is doing some response, and if the router does not find any response, the router itself may return a 404 Not Found error.

Case

Using Microsoft.AspNet.Builder; Using Microsoft.AspNet.Hosting; Using Microsoft.AspNet.Http; Using Microsoft.Extensions.DependencyInjection; Using Microsoft.Extensions.Configuration;       Namespace Firstappdemo {public class Startup {public startup () {var builder = new Configurationbuilder () .      Addjsonfile ("Appsettings.json"); Configuration = Builder.    Build ();        Public iconfiguration Configuration {get; set;}    This method gets called by the runtime.    Use this method to add services to the container. For more information on what to configure your application,//Visit http://go.microsoft.com/fwlink/? linkid=398940 public void Configureservices (Iservicecollection services) {}//This method gets the called by th    E runtime.    Use this method to configure the HTTP request pipeline. public void Configure (Iapplicationbuilder app) {app.           Useiisplatformhandler (); App. Run (Async (context) = {var msg = Configuration["message"]; Await the context.      Response.writeasync (msg);    });    }//Entry point for the application.   public static void Main (string[] args) = webapplication.run<startup> (args); } }

In the Configure () method, we will call the extension method of the Iapplicationbuilder interface to add the middleware.

By default there are two pieces of middleware in a new empty project-

Iisplatformhandler

Middleware registered with app. Run

Iisplatformhandler

Iisplatformhandler allows us to use Windows authentication. It will look at each incoming request, see if there are any Windows identity information related requests, and then call the next block middleware.

Middleware registered with app. Run

In this case a middleware in the app. The middleware registered in run. The Run method allows us to pass in another method, which we can use to handle each response. The Run method is not often what you see, we can call it a middleware terminal.

The middleware you are registering to run will never have a chance to call another middleware, and the only thing it can do is to receive the request and have some kind of reaction.

You also access a response object, and you can add some strings to the response object.

If you want to be in the app. After run to register another middleware, this middleware will never be called, because the Run method is a middleware terminal. It does not call the next block middleware.

How to add a middleware

Let's proceed to the following steps to add another middleware −

Step 1− Right-click the project and select Manage NuGet Packages.

Step 2− Search Microsoft.aspnet.diagnostics, this particular package contains many different kinds of middleware that we can use.

Step 3− If the package is not installed in your project, then choose to install this package.

Step 4− Now let's call the app in the Configure () method. Usewelcomepage Middleware.

This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure (Iapplicationbuilder app) {   app. Useiisplatformhandler ();   App. Usewelcomepage ();     App. Run (Async (context) = {    var msg = configuration["message"];    Await the context. Response.writeasync (msg);   });

Step 5− Run your application and you will see the following Welcome screen.

This welcome screen may not be so useful.

Step 6− Let's try something else that might be more useful instead of using the Welcome page and we'll use Runtimeinfopage.

This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure (Iapplicationbuilder app) {   app. Useiisplatformhandler ();   App. Useruntimeinfopage ();     App. Run (Async (context) = {    var msg = configuration["message"];    Await the context. Response.writeasync (msg);   }); }

7th Step − Save your Startup.cs page and refresh your browser, you will see the following page.

This runtimeinfopage is a middleware that will respond only to requests for a specific URL. If the incoming request does not match the URL, the middleware simply passes the request to the next piece of middleware. The request will pass through the Iisplatformhandler middleware and then go to the Useruntimeinfopage middleware. It does not create a response, so it will go to our application. Runs and displays the string.

Step 8− we add "runtimeinfo" at the end of the URL. You will now see a page that is made up of the Middleware Runtime information page.

You will see a return page that shows you some information about your runtime environment, such as the operating system, runtime version, structure, type, and all the packages you are using.

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.