Skip to content

Commit e6ff24a

Browse files
committed
updated middleware examples
1 parent 14105fd commit e6ff24a

File tree

10 files changed

+600
-4
lines changed

10 files changed

+600
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project is a boilerplate for building .NET API applications with various fe
1212
- [x] Response caching and compression
1313
- [x] Logging with Serilog
1414
- [x] Health check endpoint
15+
- [x] Middlewares
1516
- [ ] Vault Integration
1617
- [ ] MQ Integration
1718
- [ ] Application Resiliency
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class CustomHeaderMiddleware
2+
{
3+
private readonly RequestDelegate _next;
4+
5+
public CustomHeaderMiddleware(RequestDelegate next)
6+
{
7+
_next = next;
8+
}
9+
10+
public async Task Invoke(HttpContext context)
11+
{
12+
context.Response.Headers["X-Custom-Header"] = "MyCustomHeaderValue";
13+
await _next(context);
14+
}
15+
}
16+
17+
// Register in Program.cs
18+
// app.UseMiddleware<CustomHeaderMiddleware>();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class ExceptionHandlingMiddleware
2+
{
3+
private readonly RequestDelegate _next;
4+
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
5+
6+
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
7+
{
8+
_next = next;
9+
_logger = logger;
10+
}
11+
12+
public async Task Invoke(HttpContext context)
13+
{
14+
try
15+
{
16+
await _next(context);
17+
}
18+
catch (Exception ex)
19+
{
20+
_logger.LogError(ex, "An error occurred processing the request");
21+
context.Response.StatusCode = 500;
22+
await context.Response.WriteAsync("An unexpected error occurred.");
23+
}
24+
}
25+
}
26+
27+
// Register in Program.cs
28+
// app.UseMiddleware<ExceptionHandlingMiddleware>();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class IpRestrictionMiddleware
2+
{
3+
private readonly RequestDelegate _next;
4+
private readonly List<string> _blockedIps = new() { "192.168.1.100" };
5+
6+
public IpRestrictionMiddleware(RequestDelegate next)
7+
{
8+
_next = next;
9+
}
10+
11+
public async Task Invoke(HttpContext context)
12+
{
13+
var ipAddress = context.Connection.RemoteIpAddress?.ToString();
14+
if (ipAddress != null && _blockedIps.Contains(ipAddress))
15+
{
16+
context.Response.StatusCode = 403;
17+
await context.Response.WriteAsync("Access Denied.");
18+
return;
19+
}
20+
21+
await _next(context);
22+
}
23+
}
24+
25+
// Register in Program.cs
26+
// app.UseMiddleware<IpRestrictionMiddleware>();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class LoggingMiddleware
2+
{
3+
private readonly RequestDelegate _next;
4+
private readonly ILogger<LoggingMiddleware> _logger;
5+
6+
public LoggingMiddleware(RequestDelegate next, ILogger<LoggingMiddleware> logger)
7+
{
8+
_next = next;
9+
_logger = logger;
10+
}
11+
12+
public async Task Invoke(HttpContext context)
13+
{
14+
_logger.LogInformation($"Request: {context.Request.Method} {context.Request.Path}");
15+
await _next(context);
16+
_logger.LogInformation($"Response: {context.Response.StatusCode}");
17+
}
18+
}
19+
20+
// Register in Program.cs
21+
// app.UseMiddleware<LoggingMiddleware>();

0 commit comments

Comments
 (0)