Skip to content

Commit

Permalink
EHD-1411: Reduce code in Core project: Move, rename HttpContextHelper…
Browse files Browse the repository at this point in the history
… and stop using extension methods
  • Loading branch information
jamesgriff committed Nov 4, 2024
1 parent 7d51f43 commit a21577b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
}
2 changes: 1 addition & 1 deletion GenderPayGap.WebUI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a21577b

Please sign in to comment.