// 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.AspNet.Http; using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Logging; using Microsoft.AspNet.Routing.Logging.Internal; using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Builder { public class RouterMiddleware { private readonly ILogger _logger; private readonly RequestDelegate _next; private readonly IRouter _router; public RouterMiddleware( RequestDelegate next, ILoggerFactory loggerFactory, IRouter router) { _next = next; _router = router; _logger = loggerFactory.CreateLogger(); } public async Task Invoke(HttpContext httpContext) { var context = new RouteContext(httpContext); context.RouteData.Routers.Add(_router); await _router.RouteAsync(context); if (!context.IsHandled) { _logger.LogDebug("Request did not match any routes."); await _next.Invoke(httpContext); } } } }