Update .NET versions; remove deprecated package references

This commit is contained in:
Daniel J. Summers 2024-12-16 07:41:07 -05:00
parent 69d4122c1c
commit daa5629d1b
4 changed files with 21 additions and 26 deletions

2
.gitignore vendored
View File

@ -395,3 +395,5 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
.idea/

View File

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.0.0</VersionPrefix>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<VersionPrefix>1.1.0</VersionPrefix>
<PackageReleaseNotes>Update supported .NET versions; remove deprecated package references</PackageReleaseNotes>
<Authors>danieljsummers</Authors>
<Company>Bit Badger Solutions</Company>
<Description>ASP.NET Core middleware to enforce canonical domains</Description>
@ -26,8 +26,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

View File

@ -21,7 +21,7 @@ public class CanonicalDomainMiddleware
/// <summary>
/// Constructor
/// </summary>
/// <param name="next">The next middleware in the pipeline to be exectued</param>
/// <param name="next">The next middleware in the pipeline to be executed</param>
public CanonicalDomainMiddleware(RequestDelegate next)
{
_next = next;
@ -29,10 +29,12 @@ public class CanonicalDomainMiddleware
public async Task InvokeAsync(HttpContext ctx)
{
if (CanonicalDomains.ContainsKey(ctx.Request.Host.Host))
if (CanonicalDomains.TryGetValue(ctx.Request.Host.Host, out var value))
{
UriBuilder uri = new(ctx.Request.GetDisplayUrl());
uri.Host = CanonicalDomains[ctx.Request.Host.Host];
UriBuilder uri = new(ctx.Request.GetDisplayUrl())
{
Host = value
};
ctx.Response.Redirect(uri.Uri.ToString(), permanent: true);
}
else

View File

@ -39,16 +39,14 @@ public static class IApplicationBuilderExtensions
/// <param name="section">The <tt>CanonicalDomains</tt> configuration section</param>
private static void ParseConfiguration(IConfigurationSection? section)
{
if (section is not null)
if (section is null) return;
foreach (var item in section.GetChildren())
{
foreach (var item in section.GetChildren())
var nonCanonical = item["From"];
var canonical = item["To"];
if (nonCanonical is not null && canonical is not null)
{
var nonCanonical = item["From"];
var canonical = item["To"];
if (nonCanonical is not null && canonical is not null)
{
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
}
CanonicalDomainMiddleware.CanonicalDomains.Add(nonCanonical, canonical);
}
}
}
@ -57,12 +55,7 @@ public static class IApplicationBuilderExtensions
/// Generate a warning if no configured domains were found
/// </summary>
/// <param name="app">The application builder</param>
private static void WarnForMissingConfig(IApplicationBuilder app)
{
var logger = GetService<ILogger<CanonicalDomainMiddleware>>(app);
if (logger is not null)
{
logger.LogWarning("No canonical domain configuration was found; no domains will be redirected");
}
}
private static void WarnForMissingConfig(IApplicationBuilder app) =>
GetService<ILogger<CanonicalDomainMiddleware>>(app)?
.LogWarning("No canonical domain configuration was found; no domains will be redirected");
}