Member-only story
C# Extension Methods that come in handy for an ASP.Net Project
4 min readFeb 4, 2024
In the realm of C# and ASP.NET development, extension methods serve as invaluable tools, empowering developers to enhance the expressiveness and efficiency of their code. In this blog post, we will delve into some of the basic extension methods that any ASP.Net Core project needs and can elevate your web development projects.
HTTPContext Extensions
Get Base Path
public static string GetBasePath(this HttpContext httpContext)
{
return $"{httpContext.Request.Scheme}://{httpContext.Request.Host}";
}
Check if the Request Header has a particular key
public static bool HasRequestHeader(this HttpContext httpContext, string headerName)
{
return httpContext.Request.Headers.ContainsKey(headerName);
}
Check if the Response Header has a particular key
public static bool HasResponseHeader(this HttpContext httpContext, string headerName)
{
return httpContext.Response.Headers.ContainsKey(headerName);
}
Check if the Request Header key has the required value
public static bool HasRequestHeaderValue(this HttpContext httpContext…