From cdcb70233750210f9334f3e9fe801f92eb99c244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karsten=20Ku=CC=88pper?= Date: Tue, 18 Jun 2024 16:01:46 -0400 Subject: [PATCH] always redirect to bmm.bcc.media unless it has ?force-old --- BMM.Website/HtmlResult.cs | 21 +++++++++++++++++++++ BMM.Website/Program.cs | 11 ++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/BMM.Website/HtmlResult.cs b/BMM.Website/HtmlResult.cs index f8ea62b0..567cefd3 100644 --- a/BMM.Website/HtmlResult.cs +++ b/BMM.Website/HtmlResult.cs @@ -1,5 +1,6 @@ using System.Net.Mime; using System.Text; +using Microsoft.AspNetCore.Mvc; namespace BMM.Website; @@ -54,4 +55,24 @@ public async Task ExecuteAsync(HttpContext httpContext) httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(adjustedHtml); await httpContext.Response.WriteAsync(adjustedHtml); } +} + +public class RedirectResult : IResult +{ + public async Task ExecuteAsync(HttpContext httpContext) + { + var newUrl = $"https://bmm.bcc.media{httpContext.Request.Path}{httpContext.Request.QueryString}"; + + httpContext.Response.StatusCode = StatusCodes.Status302Found; + httpContext.Response.Headers.Add("Location", newUrl); + await Task.CompletedTask; + } +} + +public static class Helper +{ + public static IResult Result() + { + return new RedirectResult(); + } } \ No newline at end of file diff --git a/BMM.Website/Program.cs b/BMM.Website/Program.cs index 315f4c43..ea9adc12 100644 --- a/BMM.Website/Program.cs +++ b/BMM.Website/Program.cs @@ -1,5 +1,6 @@ using System.Globalization; using BMM.Website; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Rewrite; var builder = WebApplication.CreateBuilder(args); @@ -11,7 +12,15 @@ var indexFile = File.ReadAllText("wwwroot/index.html"); -var handler = () => new HtmlResult(indexFile); +var handler = (HttpContext context) => +{ + if (context.Request.QueryString.ToUriComponent().Contains("force-old")) + { + return new HtmlResult(indexFile); + } + + return Helper.Result(); +}; app.MapGet("/", handler); app.MapGet("index.html", handler);