// 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.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace Microsoft.AspNetCore.Mvc.RazorPages
{
///
/// Provides configuration for RazorPages.
///
public class RazorPagesOptions : IEnumerable
{
private readonly CompatibilitySwitch _allowAreas;
private readonly CompatibilitySwitch _allowMappingHeadRequestsToGetHandler;
private readonly ICompatibilitySwitch[] _switches;
private string _root = "/Pages";
public RazorPagesOptions()
{
_allowAreas = new CompatibilitySwitch(nameof(AllowAreas));
_allowMappingHeadRequestsToGetHandler = new CompatibilitySwitch(nameof(AllowMappingHeadRequestsToGetHandler));
_switches = new ICompatibilitySwitch[]
{
_allowAreas,
_allowMappingHeadRequestsToGetHandler,
};
}
///
/// Gets a collection of instances that are applied during
/// route and page model construction.
///
public PageConventionCollection Conventions { get; } = new PageConventionCollection();
///
/// Application relative path used as the root of discovery for Razor Page files.
/// Defaults to the /Pages directory under application root.
///
public string RootDirectory
{
get => _root;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(value));
}
if (value[0] != '/')
{
throw new ArgumentException(Resources.PathMustBeRootRelativePath, nameof(value));
}
_root = value;
}
}
///
/// Gets or sets a value that determines if areas are enabled for Razor Pages.
/// Defaults to false.
///
///
///
/// When enabled, any Razor Page under the directory structure /Area/AreaName/Pages/
/// will be associated with an area with the name AreaName.
///
///
/// 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 of the value 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 value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have value true unless explicitly configured.
///
///
public bool AllowAreas
{
get => _allowAreas.Value;
set => _allowAreas.Value = value;
}
///
/// Gets or sets a value that determines if HTTP method matching for Razor Pages handler methods will use
/// fuzzy matching.
/// Defaults to false.
///
///
///
/// When enabled, Razor Pages handler methods will be more flexible in which HTTP methods will be accepted
/// by GET and POST handler methods. This allows a GET handler methods to accept the HEAD HTTP methods in
/// addition to GET. A more specific handler method can still be defined to accept HEAD, and the most
/// specific handler will be invoked.
///
///
/// This setting reduces the number of handler methods that must be written to correctly respond to typical
/// web traffic including requests from internet infrastructure such as web crawlers.
///
///
/// 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 of the value 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 value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have value true unless explicitly configured.
///
///
public bool AllowMappingHeadRequestsToGetHandler
{
get => _allowMappingHeadRequestsToGetHandler.Value;
set => _allowMappingHeadRequestsToGetHandler.Value = value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_switches).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator();
}
}