// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
namespace Microsoft.AspNetCore.Mvc
{
///
/// Provides programmatic configuration for the MVC framework.
///
public class MvcOptions : IEnumerable
{
private int _maxModelStateErrors = ModelStateDictionary.DefaultMaxAllowedErrors;
// See CompatibilitySwitch.cs for guide on how to implement these.
private readonly CompatibilitySwitch _allowBindingHeaderValuesToNonStringModelTypes;
private readonly CompatibilitySwitch _allowCombiningAuthorizeFilters;
private readonly CompatibilitySwitch _allowValidatingTopLevelNodes;
private readonly CompatibilitySwitch _inputFormatterExceptionPolicy;
private readonly CompatibilitySwitch _suppressBindingUndefinedValueToEnumType;
private readonly ICompatibilitySwitch[] _switches;
///
/// Creates a new instance of .
///
public MvcOptions()
{
CacheProfiles = new Dictionary(StringComparer.OrdinalIgnoreCase);
Conventions = new List();
Filters = new FilterCollection();
FormatterMappings = new FormatterMappings();
InputFormatters = new FormatterCollection();
OutputFormatters = new FormatterCollection();
ModelBinderProviders = new List();
ModelBindingMessageProvider = new DefaultModelBindingMessageProvider();
ModelMetadataDetailsProviders = new List();
ModelValidatorProviders = new List();
ValueProviderFactories = new List();
_allowCombiningAuthorizeFilters = new CompatibilitySwitch(nameof(AllowCombiningAuthorizeFilters));
_allowBindingHeaderValuesToNonStringModelTypes = new CompatibilitySwitch(nameof(AllowBindingHeaderValuesToNonStringModelTypes));
_allowValidatingTopLevelNodes = new CompatibilitySwitch(nameof(AllowValidatingTopLevelNodes));
_inputFormatterExceptionPolicy = new CompatibilitySwitch(nameof(InputFormatterExceptionPolicy), InputFormatterExceptionPolicy.AllExceptions);
_suppressBindingUndefinedValueToEnumType = new CompatibilitySwitch(nameof(SuppressBindingUndefinedValueToEnumType));
_switches = new ICompatibilitySwitch[]
{
_allowCombiningAuthorizeFilters,
_allowBindingHeaderValuesToNonStringModelTypes,
_allowValidatingTopLevelNodes,
_inputFormatterExceptionPolicy,
_suppressBindingUndefinedValueToEnumType,
};
}
///
/// Gets or sets the flag which decides whether body model binding (for example, on an
/// action method parameter with ) should treat empty
/// input as valid. by default.
///
///
/// When , actions that model bind the request body (for example,
/// using ) will register an error in the
/// if the incoming request body is empty.
///
public bool AllowEmptyInputInBodyModelBinding { get; set; }
///
/// Gets or sets a value that determines if policies on instances of
/// will be combined into a single effective policy. The default value of the property is false.
///
///
///
/// Authorization policies are designed such that multiple authorization policies applied to an endpoint
/// should be combined and executed a single policy. The (commonly applied
/// by ) can be applied globally, to controllers, and to actions - which
/// specifies multiple authorization policies for an action. In all ASP.NET Core releases prior to 2.1
/// these multiple policies would not combine as intended. This compatibility switch configures whether the
/// old (unintended) behavior or the new combining behavior will be used when multiple authorization policies
/// are applied.
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have the value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have the value true unless explicitly configured.
///
///
public bool AllowCombiningAuthorizeFilters
{
get => _allowCombiningAuthorizeFilters.Value;
set => _allowCombiningAuthorizeFilters.Value = value;
}
///
/// Gets or sets a value that determines if should bind to types other than
/// or a collection of . If set to true,
/// would bind to simple types (like , ,
/// , etc.) or a collection of simple types. The default value of the
/// property is false.
///
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have the value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have the value true unless explicitly configured.
///
///
public bool AllowBindingHeaderValuesToNonStringModelTypes
{
get => _allowBindingHeaderValuesToNonStringModelTypes.Value;
set => _allowBindingHeaderValuesToNonStringModelTypes.Value = value;
}
///
/// Gets or sets a value that determines if model bound action parameters, controller properties, page handler
/// parameters, or page model properties are validated (in addition to validating their elements or
/// properties). If set to , and
/// ValidationAttributes on these top-level nodes are checked. Otherwise, such attributes are ignored.
///
///
/// The default value is if the version is
/// or later; otherwise.
///
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired value of the compatibility switch by calling this property's setter will take
/// precedence over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have the value unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have the value unless explicitly configured.
///
///
public bool AllowValidatingTopLevelNodes
{
get => _allowValidatingTopLevelNodes.Value;
set => _allowValidatingTopLevelNodes.Value = value;
}
///
/// Gets a Dictionary of CacheProfile Names, which are pre-defined settings for
/// response caching.
///
public IDictionary CacheProfiles { get; }
///
/// Gets a list of instances that will be applied to
/// the when discovering actions.
///
public IList Conventions { get; }
///
/// Gets a collection of which are used to construct filters that
/// apply to all actions.
///
public FilterCollection Filters { get; }
///
/// Used to specify mapping between the URL Format and corresponding media type.
///
public FormatterMappings FormatterMappings { get; }
///
/// Gets or sets a value which determines how the model binding system interprets exceptions thrown by an .
/// The default value of the property is .
///
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have the value unless
/// explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have the value
/// unless explicitly configured.
///
///
public InputFormatterExceptionPolicy InputFormatterExceptionPolicy
{
get => _inputFormatterExceptionPolicy.Value;
set => _inputFormatterExceptionPolicy.Value = value;
}
///
/// Gets a list of s that are used by this application.
///
public FormatterCollection InputFormatters { get; }
///
/// Gets or sets a value indicating whether the model binding system will bind undefined values to
/// enum types. The default value of the property is false.
///
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have the value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have the value true unless explicitly configured.
///
///
public bool SuppressBindingUndefinedValueToEnumType
{
get => _suppressBindingUndefinedValueToEnumType.Value;
set => _suppressBindingUndefinedValueToEnumType.Value = value;
}
///
/// Gets or sets the flag to buffer the request body in input formatters. Default is false.
///
public bool SuppressInputFormatterBuffering { get; set; } = false;
///
/// Gets or sets the maximum number of validation errors that are allowed by this application before further
/// errors are ignored.
///
public int MaxModelValidationErrors
{
get => _maxModelStateErrors;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_maxModelStateErrors = value;
}
}
///
/// Gets a list of s used by this application.
///
public IList ModelBinderProviders { get; }
///
/// Gets the default . Changes here are copied to the
/// property of all
/// instances unless overridden in a custom .
///
public DefaultModelBindingMessageProvider ModelBindingMessageProvider { get; }
///
/// Gets a list of instances that will be used to
/// create instances.
///
///
/// A provider should implement one or more of the following interfaces, depending on what
/// kind of details are provided:
///
///
public IList ModelMetadataDetailsProviders { get; }
///
/// Gets a list of s used by this application.
///
public IList ModelValidatorProviders { get; }
///
/// Gets a list of s that are used by this application.
///
public FormatterCollection OutputFormatters { get; }
///
/// Gets or sets the flag which causes content negotiation to ignore Accept header
/// when it contains the media type */*. by default.
///
public bool RespectBrowserAcceptHeader { get; set; }
///
/// Gets or sets the flag which decides whether an HTTP 406 Not Acceptable response
/// will be returned if no formatter has been selected to format the response.
/// by default.
///
public bool ReturnHttpNotAcceptable { get; set; }
///
/// Gets a list of used by this application.
///
public IList ValueProviderFactories { get; }
///
/// Gets or sets the SSL port that is used by this application when
/// is used. If not set the port won't be specified in the secured URL e.g. https://localhost/path.
///
public int? SslPort { get; set; }
///
/// Gets or sets the default value for the Permanent property of .
///
public bool RequireHttpsPermanent { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_switches).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator();
}
}