/// <summary> /// Branches the request pipeline based on matches of the given request path. If the request path starts with /// the given path, the branch is executed. /// </summary> /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param> /// <param name="pathMatch">The request path to match.</param> /// <param name="configuration">The branch to take for positive path matches.</param> /// <returns>The <see cref="IApplicationBuilder"/> instance.</returns> public static IApplicationBuilder Map(this IApplicationBuilder app, PathString pathMatch, Action<IApplicationBuilder> configuration) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (pathMatch.HasValue && pathMatch.Value.EndsWith("/", StringComparison.Ordinal)) { throw new ArgumentException("The path must not end with a '/'", nameof(pathMatch)); } // create branch var branchBuilder = app.New(); configuration(branchBuilder); var branch = branchBuilder.Build(); var options = new MapOptions { Branch = branch, PathMatch = pathMatch, }; return app.Use(next => new MapMiddleware(next, options).Invoke); }
public void NullArguments_ArgumentNullException() { var builder = new ApplicationBuilder(serviceProvider: null !); var noMiddleware = new ApplicationBuilder(serviceProvider: null !).Build(); var noOptions = new MapOptions(); Assert.Throws <ArgumentNullException>(() => builder.Map("/foo", configuration: null !)); Assert.Throws <ArgumentNullException>(() => new MapMiddleware(noMiddleware, null !)); }
/// <summary> /// Creates a new instance of <see cref="MapMiddleware"/>. /// </summary> /// <param name="next">The delegate representing the next middleware in the request pipeline.</param> /// <param name="options">The middleware options.</param> public MapMiddleware(RequestDelegate next, MapOptions options) { if (next == null) { throw new ArgumentNullException(nameof(next)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } _next = next; _options = options; }