// 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.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Builder
{
///
/// Provides extension methods for to add routes.
///
public static class MapRouteRouteBuilderExtensions
{
///
/// Adds a route to the with the specified name and template.
///
/// The to add the route to.
/// The name of the route.
/// The URL pattern of the route.
/// A reference to this instance after the operation has completed.
public static IRouteBuilder MapRoute(
this IRouteBuilder routeBuilder,
string name,
string template)
{
MapRoute(routeBuilder, name, template, defaults: null);
return routeBuilder;
}
///
/// Adds a route to the with the specified name, template, and default values.
///
/// The to add the route to.
/// The name of the route.
/// 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 MapRoute(
this IRouteBuilder routeBuilder,
string name,
string template,
object defaults)
{
return MapRoute(routeBuilder, name, template, defaults, constraints: null);
}
///
/// Adds a route to the with the specified name, template, default values, and
/// constraints.
///
/// The to add the route to.
/// The name of the route.
/// 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 MapRoute(
this IRouteBuilder routeBuilder,
string name,
string template,
object defaults,
object constraints)
{
return MapRoute(routeBuilder, name, template, defaults, constraints, dataTokens: null);
}
///
/// Adds a route to the with the specified name, template, default values, and
/// data tokens.
///
/// The to add the route to.
/// The name of the route.
/// 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 MapRoute(
this IRouteBuilder routeBuilder,
string name,
string template,
object defaults,
object constraints,
object dataTokens)
{
if (routeBuilder.DefaultHandler == null)
{
throw new RouteCreationException(Resources.FormatDefaultHandler_MustBeSet(nameof(IRouteBuilder)));
}
var inlineConstraintResolver = routeBuilder
.ServiceProvider
.GetRequiredService();
routeBuilder.Routes.Add(new Route(
routeBuilder.DefaultHandler,
name,
template,
new RouteValueDictionary(defaults),
new RouteValueDictionary(constraints),
new RouteValueDictionary(dataTokens),
inlineConstraintResolver));
return routeBuilder;
}
}
}