Class HttpRequestDecoratorExtensions
- Namespace
- Cuemon.AspNetCore.Http
- Assembly
- Cuemon.AspNetCore.dll
Extension methods for the HttpRequest class hidden behind the IDecorator<T> interface.
public static class HttpRequestDecoratorExtensions
- Inheritance
-
HttpRequestDecoratorExtensions
Examples
The following example demonstrates how to inspect HTTP request state using the HttpRequestDecoratorExtensions class accessed through the Decorator class.
using System;
using Cuemon;
using Cuemon.Data.Integrity;
using Cuemon.Security;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
namespace Cuemon.AspNetCore.Http;
public static class HttpRequestDecoratorExtensionsExample
{
public static void Demonstrate()
{
var context = new DefaultHttpContext();
context.Request.Method = HttpMethods.Get;
var builder = new ChecksumBuilder(() => HashFactory.CreateFnv128());
context.Request.Headers[HeaderNames.IfNoneMatch] =
string.Concat("\"", builder.Checksum.ToHexadecimalString(), "\"");
var request = Decorator.Enclose(context.Request);
var canServeFromCache = request.IsGetOrHeadMethod() && request.IsClientSideResourceCached(builder);
Console.WriteLine(canServeFromCache);
}
}
Methods
IsClientSideResourceCached(IDecorator<HttpRequest>, ChecksumBuilder)
Determines whether a cached version of the enclosed HttpRequest of the decorator is found client-side using the If-None-Match HTTP header.
public static bool IsClientSideResourceCached(this IDecorator<HttpRequest> decorator, ChecksumBuilder builder)
Parameters
decoratorIDecorator<HttpRequest>The IDecorator<T> to extend.
builderChecksumBuilderA ChecksumBuilder that represents the integrity of the client.
Returns
- bool
trueif a cached version of the enclosed HttpRequest of thedecoratoris found client-side; otherwise,false.
Exceptions
- ArgumentNullException
decoratorcannot be null -orbuildercannot be null.
IsClientSideResourceCached(IDecorator<HttpRequest>, DateTime)
Determines whether a cached version of the enclosed HttpRequest of the decorator is found client-side using the If-Modified-Since HTTP header.
public static bool IsClientSideResourceCached(this IDecorator<HttpRequest> decorator, DateTime lastModified)
Parameters
decoratorIDecorator<HttpRequest>The IDecorator<T> to extend.
lastModifiedDateTimeA DateTime value that represents the modification date of the content.
Returns
- bool
trueif a cached version of the enclosed HttpRequest of thedecoratoris found client-side; otherwise,false.
Exceptions
- ArgumentNullException
decoratorcannot be null.
IsGetOrHeadMethod(IDecorator<HttpRequest>)
Determines whether the enclosed HttpRequest of the decorator is served by either a GET or a HEAD method.
public static bool IsGetOrHeadMethod(this IDecorator<HttpRequest> decorator)
Parameters
decoratorIDecorator<HttpRequest>The IDecorator<T> to extend.
Returns
- bool
trueif the enclosed HttpRequest of thedecoratoris served by either a GET or a HEAD method; otherwise,false.
Exceptions
- ArgumentNullException
decoratorcannot be null.