-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote 4.txt
37 lines (16 loc) · 2.31 KB
/
Note 4.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
What is middleware?
It is the code that is injected into the application pipeline to handle requests and responses. They are just like chained to each other and form as a pipeline. The incoming requests are passed through this pipeline where all middleware is configured, and middleware can perform some action on the request before passing it to the next middleware. Same as for the responses, they are also passing through the middleware but in reverse order.
The main responsibilities of a middleware:
Generate an HTTP response for an incoming HTTP request
Intercept and make changes to an incoming HTTP request and pass it on to the next piece of middleware.
Intercept and make changes to an outgoing HTTP response, and pass it on to the next piece of middleware.
What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run()?
We can use both the methods while defining application request pipeline. Both are used to add middleware delegates to the application request pipeline. The middleware that is added using IApplicationBuilder.Use() may call the next middleware in the pipeline whereas the middleware that is added using the IApplicationBuilder.Run() method never calls the subsequent middleware. After IApplicationBuilder.Run method, system stop adding middleware in the request pipeline.
The IApplicationBuilder.Run() adds a terminating middleware; so no other middleware will run after the middleware added with Run().
What is the use of the "Map" extension while adding middleware to the ASP.NET Core pipeline?
It is used for branching the pipeline. It branches the ASP.NET Core pipeline based on request path matching. If the request path starts with the given path, middleware on to that branch will execute.
How do you create a custom middleware?
The custom middleware class can be used to separate the middleware code from the Program.cs (or Startup.cs in earlier versions).
You can create a custom middleware either by implementing IMiddleware interface or by using convention middleware
What is the right order of middleware used in production-level applications?
You can see how, in a typical app, existing middlewares are ordered and where custom middlewares are added. You have full control over how to reorder existing middlewares or inject new custom middlewares as necessary for your scenarios.