// 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.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Routing
{
public static class RequestDelegateRouteBuilderExtensions
{
///
/// Adds a route to the for the given , and
/// .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapRoute(this IRouteBuilder builder, string template, RequestDelegate handler)
{
var route = new Route(
new RouteHandler(handler),
template,
defaults: null,
constraints: null,
dataTokens: null,
inlineConstraintResolver: GetConstraintResolver(builder));
builder.Routes.Add(route);
return builder;
}
///
/// Adds a route to the for the given , and
/// .
///
/// The .
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewareRoute(this IRouteBuilder builder, string template, Action action)
{
var nested = builder.ApplicationBuilder.New();
action(nested);
return builder.MapRoute(template, nested.Build());
}
///
/// Adds a route to the that only matches HTTP DELETE requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapDelete(this IRouteBuilder builder, string template, RequestDelegate handler)
{
return builder.MapVerb("DELETE", template, handler);
}
///
/// Adds a route to the that only matches HTTP DELETE requests for the given
/// , and .
///
/// The .
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewareDelete(this IRouteBuilder builder, string template, Action action)
{
return builder.MapMiddlewareVerb("DELETE", template, action);
}
///
/// Adds a route to the that only matches HTTP DELETE requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapDelete(
this IRouteBuilder builder,
string template,
Func handler)
{
return builder.MapVerb("DELETE", template, handler);
}
///
/// Adds a route to the that only matches HTTP GET requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapGet(this IRouteBuilder builder, string template, RequestDelegate handler)
{
return builder.MapVerb("GET", template, handler);
}
///
/// Adds a route to the that only matches HTTP GET requests for the given
/// , and .
///
/// The .
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewareGet(this IRouteBuilder builder, string template, Action action)
{
return builder.MapMiddlewareVerb("GET", template, action);
}
///
/// Adds a route to the that only matches HTTP GET requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapGet(
this IRouteBuilder builder,
string template,
Func handler)
{
return builder.MapVerb("GET", template, handler);
}
///
/// Adds a route to the that only matches HTTP POST requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapPost(this IRouteBuilder builder, string template, RequestDelegate handler)
{
return builder.MapVerb("POST", template, handler);
}
///
/// Adds a route to the that only matches HTTP POST requests for the given
/// , and .
///
/// The .
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewarePost(this IRouteBuilder builder, string template, Action action)
{
return builder.MapMiddlewareVerb("POST", template, action);
}
///
/// Adds a route to the that only matches HTTP POST requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapPost(
this IRouteBuilder builder,
string template,
Func handler)
{
return builder.MapVerb("POST", template, handler);
}
///
/// Adds a route to the that only matches HTTP PUT requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapPut(this IRouteBuilder builder, string template, RequestDelegate handler)
{
return builder.MapVerb("PUT", template, handler);
}
///
/// Adds a route to the that only matches HTTP PUT requests for the given
/// , and .
///
/// The .
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewarePut(this IRouteBuilder builder, string template, Action action)
{
return builder.MapMiddlewareVerb("PUT", template, action);
}
///
/// Adds a route to the that only matches HTTP PUT requests for the given
/// , and .
///
/// The .
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapPut(
this IRouteBuilder builder,
string template,
Func handler)
{
return builder.MapVerb("PUT", template, handler);
}
///
/// Adds a route to the that only matches HTTP requests for the given
/// , , and .
///
/// The .
/// The HTTP verb allowed by the route.
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapVerb(
this IRouteBuilder builder,
string verb,
string template,
Func handler)
{
RequestDelegate requestDelegate = (httpContext) =>
{
return handler(httpContext.Request, httpContext.Response, httpContext.GetRouteData());
};
return builder.MapVerb(verb, template, requestDelegate);
}
///
/// Adds a route to the that only matches HTTP requests for the given
/// , , and .
///
/// The .
/// The HTTP verb allowed by the route.
/// The route template.
/// The route handler.
/// A reference to the after this operation has completed.
public static IRouteBuilder MapVerb(
this IRouteBuilder builder,
string verb,
string template,
RequestDelegate handler)
{
var route = new Route(
new RouteHandler(handler),
template,
defaults: null,
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint(verb) }),
dataTokens: null,
inlineConstraintResolver: GetConstraintResolver(builder));
builder.Routes.Add(route);
return builder;
}
///
/// Adds a route to the that only matches HTTP requests for the given
/// , , and .
///
/// The .
/// The HTTP verb allowed by the route.
/// The route template.
/// The action to apply to the .
/// A reference to the after this operation has completed.
public static IRouteBuilder MapMiddlewareVerb(
this IRouteBuilder builder,
string verb,
string template,
Action action)
{
var nested = builder.ApplicationBuilder.New();
action(nested);
return builder.MapVerb(verb, template, nested.Build());
}
private static IInlineConstraintResolver GetConstraintResolver(IRouteBuilder builder)
{
return builder.ServiceProvider.GetRequiredService();
}
}
}