// 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.AspNet.Http; using Microsoft.AspNet.Builder.Extensions; namespace Microsoft.AspNet.Builder { using Predicate = Func; /// /// Extension methods for the MapWhenMiddleware /// public static class MapWhenExtensions { /// /// Branches the request pipeline based on the result of the given predicate. /// /// /// Invoked with the request environment to determine if the branch should be taken /// Configures a branch to take /// public static IApplicationBuilder MapWhen(this IApplicationBuilder app, Predicate predicate, Action configuration) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } // create branch var branchBuilder = app.New(); configuration(branchBuilder); var branch = branchBuilder.Build(); // put middleware in pipeline var options = new MapWhenOptions { Predicate = predicate, Branch = branch, }; return app.Use(next => new MapWhenMiddleware(next, options).Invoke); } } }