/
MiddlewareTypes.razor
166 lines (135 loc) · 8.47 KB
/
MiddlewareTypes.razor
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
@page "/blogs/types-of-middleware-in-aspnet"
@inherits FragmentNavigationBase
@inject TableOfContents tableOfContents
<Content Description=@Description
Slug=@Slug
PosterPath=@PosterPath
Channel="@Channel"
ContentType="@ContentType"
TotalContents=@TotalContents
Type="Middleware"
FileName=@nameof(MiddlewareTypes)>
<ContentBody>
<p>
In this article, let's learn about different types of <ContentHighlight>Middleware</ContentHighlight> in ASP.NET.
</p>
<p>
<strong>Note:</strong> If you have not done so already, I recommend you read the article on
<NavLink class="[ underline ]" href="blogs/introducing-middleware-in-aspnet" Match="NavLinkMatch.All">Introducing Middleware in ASP.NET</NavLink>.
</p>
<h3 class="[ font-semibold text-lg ]">Table of Contents</h3>
<ol class="[ list-decimal ] [ ml-4 ]">
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#introduction")" Match="NavLinkMatch.All">
Introduction
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#run-middleware")" Match="NavLinkMatch.All">
Run Middleware
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#use-middleware")" Match="NavLinkMatch.All">
Use Middleware
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#use-when-middleware")" Match="NavLinkMatch.All">
UseWhen Middleware
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#map-middleware")" Match="NavLinkMatch.All">
Map Middleware
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#map-when-middleware")" Match="NavLinkMatch.All">
MapWhen Middleware
</NavLink>
</li>
<li>
<NavLink class="[ underline ]" href="@($"blogs/{Slug}#summary")" Match="NavLinkMatch.All">
Summary
</NavLink>
</li>
</ol>
<h3 id="introduction" class="[ font-semibold text-lg ]">Introduction</h3>
<p>
Middleware's are C# classes and logics connected by chain of responsibility pattern to handle and terminate request pipeline in the order of
appearance. Middleware's are configured by default in Startup and the order in which they run are important. Request's are processed from top
to bottom and outside to inside and back again. These are created once in Startup.
</p>
<p>
There are 5 types of middleware:
</p>
<ul class="[ list-disc ] [ ml-4 ]">
<li><ContentHighlight>Run</ContentHighlight> Middleware</li>
<li><ContentHighlight>Use</ContentHighlight> Middleware</li>
<li><ContentHighlight>UseWhen</ContentHighlight> Middleware</li>
<li><ContentHighlight>Map</ContentHighlight> Middleware</li>
<li><ContentHighlight>MapWhen</ContentHighlight> Middleware</li>
</ul>
<figure>
<img src="./image/blogs/middleware/types/AspNet Core Middleware Types.png" alt="Types of Middleware in ASP.NET" class="[ w-full ]" />
</figure>
<h3 id="run-middleware" class="[ font-semibold text-lg ]">Run Middleware</h3>
<p>
<ContentHighlight>Run</ContentHighlight> Middleware execute the delegate and terminates processing the request pipeline. Run delegates don't
receive a next parameter. The first Run delegate is always terminal and terminates the pipeline. Run should always be the last one in pipeline.
</p>
<p>
The simplest possible ASP.NET Core app sets up a single request delegate that handles all requests. This case doesn't include an actual request
pipeline. Instead, a single anonymous function is called in response to every HTTP request.
</p>
<GithubGistSnippet Title="Run Middleware" UserId="fingers10" FileName="66a05950b20165267fc97c8c07fb82e1"></GithubGistSnippet>
<h3 id="use-middleware" class="[ font-semibold text-lg ]">Use Middleware</h3>
<p>
<ContentHighlight>Use</ContentHighlight> Middleware execute the delegate and proceed to next in request pipeline. We can chain multiple request
delegates together with Use. The <ContentHighlight>next</ContentHighlight> parameter represents the next delegate in the pipeline. You can
<em>short-circuit the pipeline</em> by not calling the next parameter. You can typically perform actions both before and after the next delegate.
</p>
<GithubGistSnippet Title="Use Middleware" UserId="fingers10" FileName="6d4fb67bf040067785f7c38f34ac92c1"></GithubGistSnippet>
<p>
When a delegate doesn't pass a request to the next delegate, it's called short-circuiting the request pipeline. Short-circuiting is often desirable
because it avoids unnecessary work. For example, Static File Middleware can act as a terminal middleware by processing a request for a static file
and short-circuiting the rest of the pipeline.
</p>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="use-when-middleware" class="[ font-semibold text-lg ]">UseWhen Middleware</h3>
<p>
<ContentHighlight>UseWhen</ContentHighlight> Middleware branches the request pipeline based on the result of the given predicate and this branch is
rejoined to the main pipeline if it doesn't short-circuit or contain a terminal middleware.
</p>
<GithubGistSnippet Title="UseWhen Middleware" UserId="fingers10" FileName="b07795a4d571b38839140bd52fcdf7bd"></GithubGistSnippet>
<h3 id="map-middleware" class="[ font-semibold text-lg ]">Map Middleware</h3>
<p>
<ContentHighlight>Map</ContentHighlight> Middleware conditionally execute a method and does not return to request pipeline. Map extensions
are used as a convention for branching the pipeline. Map branches the request pipeline based on matches of the given request path. If the request
path starts with the given path, the branch is executed.
</p>
<GithubGistSnippet Title="Map Middleware" UserId="fingers10" FileName="b942378b3ba7551b3d7560c923f6dbda"></GithubGistSnippet>
<h3 id="map-when-middleware" class="[ font-semibold text-lg ]">MapWhen Middleware</h3>
<p>
<ContentHighlight>MapWhen</ContentHighlight> branches the request pipeline based on the result of the given predicate. Any predicate of type
<ContentHighlight>Func<HttpContext, bool></ContentHighlight> can be used to map requests to a new branch of the pipeline. Unlike with
<ContentHighlight>UseWhen</ContentHighlight>, this branch is not rejoined to the main pipeline.
</p>
<GithubGistSnippet Title="MapWhen Middleware" UserId="fingers10" FileName="cf512c1eb310a6bbe33b522875713796"></GithubGistSnippet>
<GoogleAdSense Type="GoogleAdSenseAdType.InArticle" Format="GoogleAdSenseAdFormat.Fluid" Style="text-align:center;" Slot="3914293965"></GoogleAdSense>
<h3 id="summary" class="[ font-semibold text-lg ]">Summary</h3>
<p>
In this article, we learn't different types <ContentHighlight>Middleware</ContentHighlight> in ASP.NET and how it processes the request pipeline
and how we can use middleware to customise the HTTP request.
</p>
</ContentBody>
</Content>
@code {
private string Description = "In this post I will teach you different types of Middleware in ASP.NET. All with live working demo.";
private string Slug = "types-of-middleware-in-aspnet";
private string PosterPath = "Blogs/Middleware";
private string Channel = "middleware";
private string ContentType = "blogs";
private ushort TotalContents => (ushort)tableOfContents.Contents.Count(content => content.Type.Equals("Middleware", StringComparison.CurrentCultureIgnoreCase));
}