// 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 Microsoft.AspNetCore.Mvc.Core; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Constraints; namespace Microsoft.AspNetCore.Builder { /// /// Extension methods for . /// public static class MvcAreaRouteBuilderExtensions { /// /// Adds a route to the with the given MVC area with the specified /// , and . /// /// The to add the route to. /// The name of the route. /// The MVC area name. /// The URL pattern of the route. /// A reference to this instance after the operation has completed. public static IRouteBuilder MapAreaRoute( this IRouteBuilder routeBuilder, string name, string areaName, string template) { MapAreaRoute(routeBuilder, name, areaName, template, defaults: null, constraints: null, dataTokens: null); return routeBuilder; } /// /// Adds a route to the with the given MVC area with the specified /// , , , and /// . /// /// The to add the route to. /// The name of the route. /// The MVC area name. /// The URL pattern of the route. /// /// An object that contains default values for route parameters. The object's properties represent the /// names and values of the default values. /// /// A reference to this instance after the operation has completed. public static IRouteBuilder MapAreaRoute( this IRouteBuilder routeBuilder, string name, string areaName, string template, object defaults) { MapAreaRoute(routeBuilder, name, areaName, template, defaults, constraints: null, dataTokens: null); return routeBuilder; } /// /// Adds a route to the with the given MVC area with the specified /// , , , /// , and . /// /// The to add the route to. /// The name of the route. /// The MVC area name. /// The URL pattern of the route. /// /// An object that contains default values for route parameters. The object's properties represent the /// names and values of the default values. /// /// /// An object that contains constraints for the route. The object's properties represent the names and /// values of the constraints. /// /// A reference to this instance after the operation has completed. public static IRouteBuilder MapAreaRoute( this IRouteBuilder routeBuilder, string name, string areaName, string template, object defaults, object constraints) { MapAreaRoute(routeBuilder, name, areaName, template, defaults, constraints, dataTokens: null); return routeBuilder; } /// /// Adds a route to the with the given MVC area with the specified /// , , , /// , , and . /// /// The to add the route to. /// The name of the route. /// The MVC area name. /// The URL pattern of the route. /// /// An object that contains default values for route parameters. The object's properties represent the /// names and values of the default values. /// /// /// An object that contains constraints for the route. The object's properties represent the names and /// values of the constraints. /// /// /// An object that contains data tokens for the route. The object's properties represent the names and /// values of the data tokens. /// /// A reference to this instance after the operation has completed. public static IRouteBuilder MapAreaRoute( this IRouteBuilder routeBuilder, string name, string areaName, string template, object defaults, object constraints, object dataTokens) { if (routeBuilder == null) { throw new ArgumentNullException(nameof(routeBuilder)); } if (string.IsNullOrEmpty(areaName)) { throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(areaName)); } var defaultsDictionary = new RouteValueDictionary(defaults); defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName; var constraintsDictionary = new RouteValueDictionary(constraints); constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName); routeBuilder.MapRoute(name, template, defaultsDictionary, constraintsDictionary, dataTokens); return routeBuilder; } } }