From a21577bcf854c9433c23935d95e5a1213293bd76 Mon Sep 17 00:00:00 2001 From: James Griffiths Date: Mon, 4 Nov 2024 13:28:13 +0000 Subject: [PATCH] EHD-1411: Reduce code in Core project: Move, rename HttpContextHelper and stop using extension methods --- .../Presentation/CompareViewService.cs | 3 +- .../Helpers/HttpContextHelper.cs | 95 ++++++++++--------- GenderPayGap.WebUI/Startup.cs | 2 +- 3 files changed, 51 insertions(+), 49 deletions(-) rename GenderPayGap.Core/Extensions/AspNetCore/HttpContext.cs => GenderPayGap.WebUI/Helpers/HttpContextHelper.cs (75%) diff --git a/GenderPayGap.WebUI/Classes/Presentation/CompareViewService.cs b/GenderPayGap.WebUI/Classes/Presentation/CompareViewService.cs index 7c5130f0d..c05feeb9c 100644 --- a/GenderPayGap.WebUI/Classes/Presentation/CompareViewService.cs +++ b/GenderPayGap.WebUI/Classes/Presentation/CompareViewService.cs @@ -83,7 +83,8 @@ public void SaveComparedEmployersToCookieIfAnyAreObfuscated() public void SaveComparedEmployersToCookie() { //Save into the cookie - httpContext.SetResponseCookie( + HttpContextHelper.SetResponseCookie( + httpContext, CookieNames.LastCompareQuery, string.Join(',', ComparedEmployers), VirtualDateTime.Now.AddMonths(1), diff --git a/GenderPayGap.Core/Extensions/AspNetCore/HttpContext.cs b/GenderPayGap.WebUI/Helpers/HttpContextHelper.cs similarity index 75% rename from GenderPayGap.Core/Extensions/AspNetCore/HttpContext.cs rename to GenderPayGap.WebUI/Helpers/HttpContextHelper.cs index 6267ee9bd..6d3fc3d77 100644 --- a/GenderPayGap.Core/Extensions/AspNetCore/HttpContext.cs +++ b/GenderPayGap.WebUI/Helpers/HttpContextHelper.cs @@ -1,47 +1,48 @@ -using Microsoft.AspNetCore.Http; - -namespace System.Web -{ - public static class HttpContext - { - - public static void SetResponseCache(this Microsoft.AspNetCore.Http.HttpContext context, int maxSeconds) - { - if (maxSeconds > 0) - { - context.Response.Headers["Cache-Control"] = $"public,max-age={maxSeconds}"; - } - else - { - context.Response.Headers["Cache-Control"] = $"no-cache,no-store,max-age=0,must-revalidate"; - } - } - - public static void SetResponseCookie(this Microsoft.AspNetCore.Http.HttpContext context, - string key, - string value, - DateTime expires, - string subdomain = null, - string path = "/", - bool httpOnly = false, - bool secure = false) - { - var cookieOptions = new CookieOptions { - Expires = expires, - Domain = subdomain, - Path = path, - Secure = secure, - HttpOnly = httpOnly - }; - if (string.IsNullOrWhiteSpace(value)) - { - context.Response.Cookies.Delete(key); - } - else - { - context.Response.Cookies.Append(key, value, cookieOptions); - } - } - - } -} +using System; +using Microsoft.AspNetCore.Http; + +namespace GenderPayGap.WebUI.Helpers +{ + public static class HttpContextHelper + { + + public static void SetResponseCache(HttpContext context, int maxSeconds) + { + if (maxSeconds > 0) + { + context.Response.Headers["Cache-Control"] = $"public,max-age={maxSeconds}"; + } + else + { + context.Response.Headers["Cache-Control"] = $"no-cache,no-store,max-age=0,must-revalidate"; + } + } + + public static void SetResponseCookie(HttpContext context, + string key, + string value, + DateTime expires, + string subdomain = null, + string path = "/", + bool httpOnly = false, + bool secure = false) + { + var cookieOptions = new CookieOptions { + Expires = expires, + Domain = subdomain, + Path = path, + Secure = secure, + HttpOnly = httpOnly + }; + if (string.IsNullOrWhiteSpace(value)) + { + context.Response.Cookies.Delete(key); + } + else + { + context.Response.Cookies.Append(key, value, cookieOptions); + } + } + + } +} diff --git a/GenderPayGap.WebUI/Startup.cs b/GenderPayGap.WebUI/Startup.cs index b077be025..5ab842311 100644 --- a/GenderPayGap.WebUI/Startup.cs +++ b/GenderPayGap.WebUI/Startup.cs @@ -274,7 +274,7 @@ public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime) //Caching static files is required to reduce connections since the default behavior of checking if a static file has changed and returning a 304 still requires a connection. if (Global.StaticCacheSeconds > 0) { - ctx.Context.SetResponseCache(Global.StaticCacheSeconds); + HttpContextHelper.SetResponseCache(ctx.Context, Global.StaticCacheSeconds); } } }); //For the wwwroot folder