Table of Contents

Class MvcBuilderExtensions

Namespace
Cuemon.Extensions.AspNetCore.Mvc.Filters
Assembly
Cuemon.Extensions.AspNetCore.Mvc.dll

Extension methods for the IMvcBuilder interface.

public static class MvcBuilderExtensions
Inheritance
MvcBuilderExtensions

Examples

The following example configures the MVC builder with the same option families covered by the unit tests: API-key enforcement, throttling, user-agent validation, fault descriptors, and cache headers. It chains AddApiKeySentinelOptions, AddThrottlingSentinelOptions, AddUserAgentSentinelOptions, AddFaultDescriptorOptions, and AddHttpCacheableOptions on the MVC builder. The resulting service count is written to the console, demonstrating how to register security, throttling, and caching middleware in a single fluent configuration.

using System;
using Cuemon;
using Cuemon.AspNetCore.Http;
using Cuemon.AspNetCore.Http.Throttling;
using Cuemon.Diagnostics;
using Cuemon.Extensions.AspNetCore.Mvc.Filters;
using Cuemon.Extensions.AspNetCore.Mvc.Filters.Cacheable;
using Cuemon.Extensions.AspNetCore.Mvc.Filters.Diagnostics;
using Microsoft.Extensions.DependencyInjection;

namespace Cuemon.Extensions.AspNetCore.Mvc.Filters.DocExamples;

public sealed class MvcBuilderExtensionsExample
{
    public IServiceCollection ConfigureServices()
    {
        var services = new ServiceCollection();

        var builder = services
            .AddMvc()
            .AddApiKeySentinelOptions(options =>
            {
                options.AllowedKeys.Add("demo-key");
                options.UseGenericResponse = true;
            })
            .AddThrottlingSentinelOptions(options =>
            {
                options.ContextResolver = context => context.Connection.RemoteIpAddress?.ToString() ?? "anonymous";
                options.Quota = new ThrottleQuota(100, 1, TimeUnit.Minutes);
                options.TooManyRequestsMessage = "Rate limit exceeded.";
            })
            .AddUserAgentSentinelOptions(options =>
            {
                options.RequireUserAgentHeader = true;
                options.ValidateUserAgentHeader = true;
                options.AllowedUserAgents.Add("DocsSample/1.0");
            })
            .AddFaultDescriptorOptions(options =>
            {
                options.MarkExceptionHandled = true;
                options.SensitivityDetails = FaultSensitivityDetails.All;
                options.HttpFaultResolvers.AddHttpFaultResolver<TooManyRequestsException>();
            })
            .AddHttpCacheableOptions(options =>
            {
                options.CacheControl.MaxAge = TimeSpan.FromMinutes(5);
                options.Filters.AddLastModifiedHeader();
                options.Filters.AddEntityTagHeader(entityTag =>
                {
                    entityTag.UseEntityTagResponseParser = true;
                });
            });

        Console.WriteLine(builder.Services.Count);
        return builder.Services;
    }
}

Methods

AddApiKeySentinelOptions(IMvcBuilder, Action<ApiKeySentinelOptions>)

Registers the specified setup to configure ApiKeySentinelOptions in the underlying service collection of builder.

public static IMvcBuilder AddApiKeySentinelOptions(this IMvcBuilder builder, Action<ApiKeySentinelOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder to extend.

setup Action<ApiKeySentinelOptions>

The ApiKeySentinelOptions which may be configured.

Returns

IMvcBuilder

A reference to builder so that additional configuration calls can be chained.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of ApiKeySentinelOptions in a valid state.

AddFaultDescriptorOptions(IMvcBuilder, Action<MvcFaultDescriptorOptions>)

Registers the specified setup to configure MvcFaultDescriptorOptions in the underlying service collection of builder.

public static IMvcBuilder AddFaultDescriptorOptions(this IMvcBuilder builder, Action<MvcFaultDescriptorOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder to extend.

setup Action<MvcFaultDescriptorOptions>

The MvcFaultDescriptorOptions that may be configured.

Returns

IMvcBuilder

A reference to builder so that additional configuration calls can be chained.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of MvcFaultDescriptorOptions in a valid state.

AddHttpCacheableOptions(IMvcBuilder, Action<HttpCacheableOptions>)

Registers the specified setup to configure HttpCacheableOptions in the underlying service collection of builder.

public static IMvcBuilder AddHttpCacheableOptions(this IMvcBuilder builder, Action<HttpCacheableOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder to extend.

setup Action<HttpCacheableOptions>

The HttpCacheableOptions which may be configured.

Returns

IMvcBuilder

A reference to builder so that additional configuration calls can be chained.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of HttpCacheableOptions in a valid state.

AddThrottlingSentinelOptions(IMvcBuilder, Action<ThrottlingSentinelOptions>)

Registers the specified setup to configure ThrottlingSentinelOptions in the underlying service collection of builder.

public static IMvcBuilder AddThrottlingSentinelOptions(this IMvcBuilder builder, Action<ThrottlingSentinelOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder to extend.

setup Action<ThrottlingSentinelOptions>

The ThrottlingSentinelOptions which may be configured.

Returns

IMvcBuilder

A reference to builder so that additional configuration calls can be chained.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of ThrottlingSentinelOptions in a valid state.

AddUserAgentSentinelOptions(IMvcBuilder, Action<UserAgentSentinelOptions>)

Registers the specified setup to configure UserAgentSentinelOptions in the underlying service collection of builder.

public static IMvcBuilder AddUserAgentSentinelOptions(this IMvcBuilder builder, Action<UserAgentSentinelOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder to extend.

setup Action<UserAgentSentinelOptions>

The UserAgentSentinelOptions which may be configured.

Returns

IMvcBuilder

A reference to builder so that additional configuration calls can be chained.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of UserAgentSentinelOptions in a valid state.