Skip to content

tom1978-coder/RedirectMiddleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RedirectMiddlewareApp

A .NET 8 Razor Pages / MVC hybrid sample demonstrating a lightweight URL redirection system backed by an in-memory cache and a pluggable redirect source. It shows how to centralize legacy URL handling without scattering rewrite logic across the app.

Key Features

  • Central RedirectUrlService for resolving old -> new URLs.
  • Pluggable IRedirectUrlProvider so redirects can come from config, database, JSON, etc.
  • In-memory caching via IMemoryCache with configurable refresh + expiration.
  • Periodic background refresh using System.Threading.Timer.
  • Supports absolute or relative target URLs (URLTypeEnum).
  • Clean separation in MiddlewareLibrary for reuse across applications.

How It Works

  1. A request arrives.
  2. Your middleware or controller (you add this) calls IRedirectUrlService.GetRedirect(path).
  3. If a match exists, you issue a 301/302 to Redirect.NewUrl.
  4. The service maintains a cached list of Redirect entries, auto-refreshed on an interval.

Redirect Model

Caching Behavior

  • Refresh interval: RedirectCacheRefreshMinutes (background timer repopulates cache).
  • Expiration: RedirectCacheExpireMinutes (absolute expiration for the cache entry).
  • If the cache is empty on first use, it is populated synchronously.

Configure both to balance memory freshness vs. performance.

Configuration

In appsettings.json (example):

Service Registration (Program.cs)

Implementing a Redirect Provider

Provide your source of truth by implementing IRedirectUrlProvider:

Using the Service (Example Middleware Sketch)

public class RedirectMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IRedirectUrlService _redirectUrlService;

    public RedirectMiddleware(RequestDelegate next, IRedirectUrlService redirectUrlService)
    {
        _next = next;
        _redirectUrlService = redirectUrlService;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var path = context.Request.Path.Value;
        var redirect = _redirectUrlService.GetRedirect(path);

        if (redirect != null)
        {
            context.Response.Redirect(redirect.NewUrl, redirect.Permanently);
            return;
        }

        await _next(context);
    }
}

Navigate to: https://localhost:5001 (or shown port).

Logging

RedirectUrlService logs:

  • Cache refresh operations.
  • Cache hits.
  • Failed provider calls (if exceptions logged externally).

Third-Party Client Libraries

Bootstrap, jQuery, and validation scripts are included under their respective MIT licenses (see /wwwroot/lib/*/LICENSE).

License

This sample code is MIT licensed (adjust if you add a license file). Third-party libraries retain their original licenses.

Next Steps

  • Add real redirect provider.
  • Add unit tests around RedirectUrlService.
  • Add admin UI for managing redirects (optional).

Happy building!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published