Skip to content

Commit 09f80b1

Browse files
committed
Refactor RedirectMiddleware and NotFoundMiddleware to use primary constructors, removing redundant fields
1 parent e36b3c0 commit 09f80b1

2 files changed

Lines changed: 4 additions & 18 deletions

File tree

server/src/Docs.Web/Middleware/NotFoundMiddleware.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@ namespace Docs.Web.Middleware;
33
/// <summary>
44
/// Middleware that serves a custom 404.html page when the response status code is 404.
55
/// </summary>
6-
public class NotFoundMiddleware : IMiddleware
6+
public class NotFoundMiddleware(IWebHostEnvironment environment) : IMiddleware
77
{
8-
private readonly IWebHostEnvironment _environment;
9-
10-
public NotFoundMiddleware(IWebHostEnvironment environment)
11-
{
12-
_environment = environment;
13-
}
14-
158
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
169
{
1710
await next(context);
1811

1912
if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
2013
{
21-
var notFoundPath = Path.Combine(_environment.WebRootPath, "404.html");
14+
var notFoundPath = Path.Combine(environment.WebRootPath, "404.html");
2215

2316
if (File.Exists(notFoundPath))
2417
{

server/src/Docs.Web/Middleware/RedirectMiddleware.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ namespace Docs.Web.Middleware;
33
/// <summary>
44
/// Middleware that redirects old URLs to new destinations using a preloaded redirect map (301 permanent).
55
/// </summary>
6-
public class RedirectMiddleware : IMiddleware
6+
public class RedirectMiddleware(IReadOnlyDictionary<string, string> redirectMap) : IMiddleware
77
{
8-
private readonly IReadOnlyDictionary<string, string> _redirectMap;
9-
10-
public RedirectMiddleware(IReadOnlyDictionary<string, string> redirectMap)
11-
{
12-
_redirectMap = redirectMap;
13-
}
14-
158
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
169
{
1710
var path = context.Request.Path.Value?.TrimEnd('/') ?? "";
1811

19-
if (_redirectMap.TryGetValue(path, out var destination))
12+
if (redirectMap.TryGetValue(path, out var destination))
2013
{
2114
var queryString = context.Request.QueryString.HasValue ? context.Request.QueryString.Value : "";
2215
context.Response.StatusCode = 301;

0 commit comments

Comments
 (0)