-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
157 lines (135 loc) · 5.59 KB
/
Startup.cs
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
using Microsoft.AspNetCore.Builder;
namespace Middleware
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddEndpointsApiExplorer();
}
public void Configure(IApplicationBuilder app)
{
//Typical middleware order
//Catches exception thrown in following middlewares
//app.UseExceptionHandler();
//Adds Strict-Transport-Security header
app.UseHsts();
//Redirects HTTP requests to HTTPS
app.UseHttpsRedirection();
//Returns static files and short-circuits further request processing
app.UseStaticFiles();
//This is where routing decisions are made. Should be used in conjunction with UseEndpoints middleware
app.UseRouting();
//Setup CORS
app.UseCors();
//Authentication
app.UseAuthentication();
//Authorization
app.UseAuthorization();
//Custom middlewares go here
//Anonymous middleware
app.Use(
async (httpCtx, next) =>
{
//Do some work with httpcontext.Request
await next.Invoke();
// Do something with httpContext.Response (Logging etc.)
});
//Use - Inserts middleware in pipeline. This usecase is of short-circuiting a request based on path-match
app.Use(
async (httpCtx, next) =>
{
if (httpCtx.Request.Path.StartsWithSegments("/short-circuit"))
{
await httpCtx.Response.WriteAsync("Request terminated by short-circuit middleware.");
}
else
{
await next();
}
});
//UseWhen - More focused on conditionally executing request based on HttpContext
app.UseWhen(context => context.Request.Path.StartsWithSegments("/use-when"),
branch =>
{
//Works
//branch.Use(async (context, next) =>
//{
// if (context.Request.Path.StartsWithSegments("/use-when"))
// {
// await context.Response.WriteAsync("Request terminated by use-when middleware");
// }
// else
// {
// await next();
// }
//});
//Run is better for Short-circuiting
branch.Run(async (context) =>
{
await context.Response.WriteAsync("Request terminated by use-when middleware");
});
});
//Map - More focused on conditionally executing middleware based on Request Path. Problematic code
//app.Map("/map-test",
// branch =>
// {
// branch.Use(async (context, next) =>
// {
// if (context.Request.Path.StartsWithSegments("/map-test"))
// {
// await context.Response.WriteAsync("Request terminated by map middleware");
// }
// else
// {
// await next();
// }
// });
// });
//Solved
// When using Use inside Map, it doesn't automatically continue the pipeline for the original request; instead, it essentially replaces the remainder of the pipeline for that branch.
app.Map("/map",
branch =>
{
branch.Run(async (context) =>
{
await context.Response.WriteAsync("Request terminated by map middleware");
});
});
//Map more focused on Path.
app.MapWhen(context => context.Request.Path.StartsWithSegments("/map-when"),
branch =>
{
branch.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/map-when"))
{
await context.Response.WriteAsync("Request terminated by map-when middleware");
}
else
{
await next();
}
});
});
//Enabling this will terminate all requests here if route is /ok or /internal-server-error
//app.Run(async context =>
//{
// await context.Response.WriteAsync("Request terminated at Run middleware");
//});
app.UseEndpoints(app =>
{
app.MapGet("/ok", context =>
{
return context.Response.WriteAsync($"Request received: {context.Request.Path.Value}");
});
app.MapGet("/internal-server-error", context =>
{
context.Response.StatusCode = 500;
return context.Response.WriteAsync("Something went wrong");
});
});
}
}
}