// 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.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; namespace Microsoft.AspNetCore.Mvc.ApplicationModels { [DebuggerDisplay("Name={ControllerName}, Type={ControllerType.Name}")] public class ControllerModel : ICommonModel, IFilterModel, IApiExplorerModel { public ControllerModel( TypeInfo controllerType, IReadOnlyList attributes) { if (controllerType == null) { throw new ArgumentNullException(nameof(controllerType)); } if (attributes == null) { throw new ArgumentNullException(nameof(attributes)); } ControllerType = controllerType; Actions = new List(); ApiExplorer = new ApiExplorerModel(); Attributes = new List(attributes); ControllerProperties = new List(); Filters = new List(); Properties = new Dictionary(); RouteValues = new Dictionary(StringComparer.OrdinalIgnoreCase); Selectors = new List(); } public ControllerModel(ControllerModel other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } ControllerName = other.ControllerName; ControllerType = other.ControllerType; // Still part of the same application Application = other.Application; // These are just metadata, safe to create new collections Attributes = new List(other.Attributes); Filters = new List(other.Filters); RouteValues = new Dictionary(other.RouteValues, StringComparer.OrdinalIgnoreCase); Properties = new Dictionary(other.Properties); // Make a deep copy of other 'model' types. Actions = new List(other.Actions.Select(a => new ActionModel(a))); ApiExplorer = new ApiExplorerModel(other.ApiExplorer); ControllerProperties = new List(other.ControllerProperties.Select(p => new PropertyModel(p))); Selectors = new List(other.Selectors.Select(s => new SelectorModel(s))); } public IList Actions { get; } /// /// Gets or sets the for this controller. /// /// /// allows configuration of settings for ApiExplorer /// which apply to all actions in the controller unless overridden by . /// /// Settings applied by override settings from /// . /// public ApiExplorerModel ApiExplorer { get; set; } public ApplicationModel Application { get; set; } public IReadOnlyList Attributes { get; } MemberInfo ICommonModel.MemberInfo => ControllerType; string ICommonModel.Name => ControllerName; public string ControllerName { get; set; } public TypeInfo ControllerType { get; } public IList ControllerProperties { get; } public IList Filters { get; } /// /// Gets a collection of route values that must be present in the /// for the corresponding action to be selected. /// /// /// Entries in can be overridden by entries in /// . /// public IDictionary RouteValues { get; } /// /// Gets a set of properties associated with the controller. /// These properties will be copied to . /// /// /// Entries will take precedence over entries with the same key /// in . /// public IDictionary Properties { get; } public IList Selectors { get; } } }