ASP. net mvc-create custom Middleware, mvcmiddleware

Source: Internet
Author: User

ASP. net mvc-create custom Middleware, mvcmiddleware

After the introduction in the first two articles, I believe you have a basic understanding of OWIN and Katana. In this article, I will continue my journey to OWIN and Katana-creating custom Middleware.

What is Middleware?

The Middleware can be understood as a function used to process Http requests. When the Server encapsulates Http requests into a dictionary that complies with the OWIN specification, it is handed over to Middleware for processing. Generally, the Middleware in Pipeline processes Http requests in a chain. That is, each Middleware is the smallest modular and independent of each other and efficient.

In terms of syntax, Middleware is an application delegate (Func <IDictionary <string,Object>,Task>), insert a Middleware into the Pipeline by using the Use or Run method of the IAppBuilder interface. The difference is that the Run method does not need to reference the next Middleware, this is the final processing element in Pipeline.

Use Inline to register Middleware

You can Use the Use method to insert a Middleware into the Pipeline. It is worth noting that you need to pass in the reference of the next Middleware. The Code is as follows:

In the above Code, a delegate is instantiated. It needs to pass in the Middleware reference in the next Pipeline and return a new Middleware and insert it into the Pipeline. Because it is asynchronous, do not forget the async and await keywords.

Use Inline + AppFunc to register Middleware

To simplify writing, I delegate (Func <IDictionary <string,Object>,Task>) created the alias AppFunc:

You can use the following method to add Middleware to Pipeline:

Considering the growth of business logic, it is necessary to separate the processing logic in Lambda expressions. Therefore, the code above is slightly modified and extracted to a method named Invoke:

Although the business logic is extracted into a method, the Inline mode is not concise and easy to understand for complicated Middleware. We prefer to create a separate class for representation.

Define the native Middleware class to register Middleware

If you only want to track requests, using Inline is also feasible, but for complicated Middleware, I prefer to create a separate class, as shown below:

Finally, we still Use the Use method to add Middleware to the Pipeline:

In the above Code, the Use method of the IAppBuilder instance adds Middleware to Pipeline, which is very different from the Inline method. It accepts a Type rather than a Lambda expression. In this case, an instance of the Middleware type is created, and the next Middleware in the Pipeline is passed to the constructor. Finally, the Invoke method is called when the Middleware is executed.

Note that the Middleware is defined in the agreed form and must meet the following conditions:

  • The first parameter of the constructor must be the next Middleware in Pipeline.
  • Must contain an Invoke method, which receives the Owin environment dictionary and returns the Task
Use Katana Helper to register Middleware

Assembly Microsoft. owin contains the Helper provided by Katana for us. It can simplify our development. For example, IOwinContext encapsulates the Owin environment dictionary, and a strong type object can obtain relevant data through attributes, at the same time, it provides a variety of extension methods for IAppBuilder to simplify Middleware registration, as shown below:

Of course, we can also define a Middleware class and inherit OwinMiddleware, as shown below:

Then add it to the Pipeline:

Execution sequence of Middleware

After completing the Middleware registration, add the last Middleware at the end of the Configuration method. Note that it does not need to be referenced by the next Middleware. We can use the Run method to complete registration:

It is worth noting that the Http Request processing order of Middleware in Pipeline is consistent with the registration order, that is, the order written in the Configuration method is the same, and the Response order is the opposite, as shown in:

Finally, run the program to check whether the specific output result is consistent with our analysis:

Summary

In this article, I explained how to create custom Middleware. Katana provides us with many ways to create and register Middleware. In the next article, I will continue my journey to OWIN and Katana and explore the integration of Katana and other Web frameworks.

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.