Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Map/MapWhen extension method in .NET Core

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use the Map/MapWhen extension method in. NET Core. The content is concise and easy to understand. It will definitely make you shine. I hope you can gain something from the detailed introduction of this article.

0x00 Why Map (MapWhen) Extension is Needed

If the business logic is relatively simple, a main pipeline is enough, and Map is really not needed. However, when the business logic is complex, sometimes we may want to use a special set of middleware to handle HttpContext, depending on the situation. In this case, if only one pipe is used, it will be very troublesome and confusing to deal with. At this point, you can use Map/MapWhen to create a branch pipeline, when the conditions meet our settings, this branch pipeline will handle HttpContext. Adding branch pipes is easy using Map/MapWhen, as long as you provide the logic to jump to the branch pipe properly and the construction method of the branch pipe.

0x01 Map Extension Principle

As we said in the previous article, middleware registration and pipeline construction are done through Application Builder. So to build a branch pipeline, you need a new Application Builder and use it to register the middleware to build the pipeline. In order to share the services registered in the current ApplicationBuilder in the branch pipeline (or share the dependency injection container, of course, there are more than these), when creating a new ApplicationBuilder, we do not directly create a new one, but call the New method of the current ApplicationBuilder to create a new one based on the current one, sharing the Properties of the current ApplicationBuilder (which contains the dependency injection container).

When registering middleware using Map we pass in an Action parameter, its role is, when we create a new Application Builder, use this method to make various settings on it, the most important thing is to register the middleware of the branch pipeline on the new Application Builder. After the configuration is completed, call the Builder method of Branch ApplicationBuilder to build the pipeline, and save the first middleware as the entry of the branch pipeline.

When registering middleware with Map, a PathString parameter is passed, and the PathString object we can simply think of as string. It is used to record the segment to match in HttpContext.HttpRequest. Path. This string argument cannot end with "/". If the match is successful, enter the branch pipeline, if the match is lost, continue the current pipeline.

The newly constructed pipe and the string used for matching are saved as MapOptions objects, which hold Map rules and entries for branching pipes. Then build the MapMiddleware object and wrap its Invoke method as RequestDelegate, registering the middleware using the current ApplicationBuilder's Use method.

Here is how Map expands:

Here is MapMiddleware's Invoke method

0x02 MapWhen Extension Principle

Map mainly determines whether to enter the branch pipeline by the Path in the URL, but sometimes we may have other requirements, for example, I want to use a special pipeline for all requests with Delete Method. You need to use MapWhen. MapWhen is a generic Map that allows users to decide when to enter branch pipes and when not to enter. It can be said that Map is a case of MapWhen, because this case is too common, so the official implementation of one. So MapWhen is very simple, in Map we pass parameter PathString to match HttpRequest.Path, in MapWhen we pass parameter Func, we specify ourselves, when it returns true to enter the branch pipeline, return false to continue the current pipeline.

Here's how MapWhen expands:

Here is MapWhenMiddleware's Invoke method:

0x03 By the way, Run Extension

Another extension to registering middleware is Run, which is mentioned last because it's so simple. Run is to register a middleware with Use and return directly without calling the next middleware. So using the Run extension method to register middleware is always directly returned, and the latter middleware will not be called.

This is how to use Map/MapWhen extension methods in. NET Core. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report