Class HttpRequestExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Http
- Assembly
- Cuemon.Extensions.AspNetCore.dll
Extension methods for the HttpRequest class.
public static class HttpRequestExtensions
- Inheritance
-
HttpRequestExtensions
Examples
HttpRequestExtensions provides extension methods for HttpRequest that inspect accepted MIME types, HTTP method type, and client-side caching status. This example sets up a request with Accept header values (text/html, application/json, */* with quality scores), If-None-Match with an ETag, and If-Modified-Since with a date, then calls AcceptMimeTypesOrderedByQuality to sort by q-value, IsGetOrHeadMethod to check the HTTP method, and IsClientSideResourceCached with both a ChecksumBuilder ETag and a Last-Modified date overload. Console output shows the sorted MIME types (application/json, text/html, */*), the method check results, and the cache state booleans.
using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Data.Integrity;
using Cuemon.Extensions.AspNetCore.Http;
using Cuemon.Security;
using Microsoft.AspNetCore.Http;
namespace MyApp.AspNetCore.Http
{
public class HttpRequestExtensionsExample
{
public void Demonstrate(HttpRequest request)
{
// Get ordered MIME types from the Accept header by quality value
request.Headers["Accept"] = "text/html;q=0.8, application/json;q=0.9, */*;q=0.1";
IEnumerable<string> preferredTypes = request.AcceptMimeTypesOrderedByQuality();
Console.WriteLine(string.Join(", ", preferredTypes));
// Output: "application/json, text/html, */*" (sorted by q-value descending)
// Check if the request uses GET or HEAD method
request.Method = "GET";
bool isGetOrHead = request.IsGetOrHeadMethod();
Console.WriteLine(isGetOrHead); // True
request.Method = "POST";
isGetOrHead = request.IsGetOrHeadMethod();
Console.WriteLine(isGetOrHead); // False
// Check if the client has a cached version using If-None-Match (ETag)
request.Method = "GET";
request.Headers["If-None-Match"] = "\"abc123\"";
var checksumBuilder = new ChecksumBuilder(() => HashFactory.CreateFnv128());
bool isCached = request.IsClientSideResourceCached(checksumBuilder);
Console.WriteLine(isCached); // True or False depending on checksum match
// Check if the client has a cached version using If-Modified-Since
request.Headers["If-Modified-Since"] = "Tue, 15 Jun 2024 10:00:00 GMT";
var lastModified = new DateTime(2024, 6, 15, 9, 0, 0, DateTimeKind.Utc);
isCached = request.IsClientSideResourceCached(lastModified);
Console.WriteLine(isCached); // False (resource modified after If-Modified-Since date)
}}
}
Methods
AcceptMimeTypesOrderedByQuality(HttpRequest)
Returns the MIME type types from the HTTP Accept header of the request, ordered by their quality values, e.g., preferred MIME types are first.
public static IEnumerable<string> AcceptMimeTypesOrderedByQuality(this HttpRequest request)
Parameters
requestHttpRequestThe HttpRequest to extend.
Returns
- IEnumerable<string>
A sequence of strings representing the MIME types from the HTTP Accept header of the
request, ordered by their quality values.
IsClientSideResourceCached(HttpRequest, ChecksumBuilder)
Determines whether a cached version of the requested resource is found client-side using the If-None-Match HTTP header.
public static bool IsClientSideResourceCached(this HttpRequest request, ChecksumBuilder builder)
Parameters
requestHttpRequestAn instance of the HttpRequest object.
builderChecksumBuilderA ChecksumBuilder that represents the integrity of the client.
Returns
- bool
trueif a cached version of the requested content is found client-side; otherwise,false.
Exceptions
- ArgumentNullException
requestcannot be null -orbuildercannot be null.
IsClientSideResourceCached(HttpRequest, DateTime)
Determines whether a cached version of the requested resource is found client-side using the If-Modified-Since HTTP header.
public static bool IsClientSideResourceCached(this HttpRequest request, DateTime lastModified)
Parameters
requestHttpRequestAn instance of the HttpRequest object.
lastModifiedDateTimeA DateTime value that represents the modification date of the content.
Returns
- bool
trueif a cached version of the requested content is found client-side; otherwise,false.
Exceptions
- ArgumentNullException
requestcannot be null.
IsGetOrHeadMethod(HttpRequest)
Determines whether the specified request is served by either a GET or a HEAD method.
public static bool IsGetOrHeadMethod(this HttpRequest request)
Parameters
requestHttpRequestAn instance of the HttpRequest object.
Returns
- bool
trueif the specifiedrequestis served by either a GET or a HEAD method; otherwise,false.
Exceptions
- ArgumentNullException
requestcannot be null.