127 lines
5.5 KiB
C#
127 lines
5.5 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for <see cref="IRouteBuilder"/> to add routes.
|
|
/// </summary>
|
|
public static class MapRouteRouteBuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name and template.
|
|
/// </summary>
|
|
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
|
|
/// <param name="name">The name of the route.</param>
|
|
/// <param name="template">The URL pattern of the route.</param>
|
|
/// <returns>A reference to this instance after the operation has completed.</returns>
|
|
public static IRouteBuilder MapRoute(
|
|
this IRouteBuilder routeBuilder,
|
|
string name,
|
|
string template)
|
|
{
|
|
MapRoute(routeBuilder, name, template, defaults: null);
|
|
return routeBuilder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, and default values.
|
|
/// </summary>
|
|
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
|
|
/// <param name="name">The name of the route.</param>
|
|
/// <param name="template">The URL pattern of the route.</param>
|
|
/// <param name="defaults">
|
|
/// An object that contains default values for route parameters. The object's properties represent the names
|
|
/// and values of the default values.
|
|
/// </param>
|
|
/// <returns>A reference to this instance after the operation has completed.</returns>
|
|
public static IRouteBuilder MapRoute(
|
|
this IRouteBuilder routeBuilder,
|
|
string name,
|
|
string template,
|
|
object defaults)
|
|
{
|
|
return MapRoute(routeBuilder, name, template, defaults, constraints: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, default values, and
|
|
/// constraints.
|
|
/// </summary>
|
|
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
|
|
/// <param name="name">The name of the route.</param>
|
|
/// <param name="template">The URL pattern of the route.</param>
|
|
/// <param name="defaults">
|
|
/// An object that contains default values for route parameters. The object's properties represent the names
|
|
/// and values of the default values.
|
|
/// </param>
|
|
/// <param name="constraints">
|
|
/// An object that contains constraints for the route. The object's properties represent the names and values
|
|
/// of the constraints.
|
|
/// </param>
|
|
/// <returns>A reference to this instance after the operation has completed.</returns>
|
|
public static IRouteBuilder MapRoute(
|
|
this IRouteBuilder routeBuilder,
|
|
string name,
|
|
string template,
|
|
object defaults,
|
|
object constraints)
|
|
{
|
|
return MapRoute(routeBuilder, name, template, defaults, constraints, dataTokens: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a route to the <see cref="IRouteBuilder"/> with the specified name, template, default values, and
|
|
/// data tokens.
|
|
/// </summary>
|
|
/// <param name="routeBuilder">The <see cref="IRouteBuilder"/> to add the route to.</param>
|
|
/// <param name="name">The name of the route.</param>
|
|
/// <param name="template">The URL pattern of the route.</param>
|
|
/// <param name="defaults">
|
|
/// An object that contains default values for route parameters. The object's properties represent the names
|
|
/// and values of the default values.
|
|
/// </param>
|
|
/// <param name="constraints">
|
|
/// An object that contains constraints for the route. The object's properties represent the names and values
|
|
/// of the constraints.
|
|
/// </param>
|
|
/// <param name="dataTokens">
|
|
/// An object that contains data tokens for the route. The object's properties represent the names and values
|
|
/// of the data tokens.
|
|
/// </param>
|
|
/// <returns>A reference to this instance after the operation has completed.</returns>
|
|
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<IInlineConstraintResolver>();
|
|
|
|
routeBuilder.Routes.Add(new Route(
|
|
routeBuilder.DefaultHandler,
|
|
name,
|
|
template,
|
|
new RouteValueDictionary(defaults),
|
|
new RouteValueDictionary(constraints),
|
|
new RouteValueDictionary(dataTokens),
|
|
inlineConstraintResolver));
|
|
|
|
return routeBuilder;
|
|
}
|
|
}
|
|
}
|