public async Task Invoke(HttpContext context) { PathString subpath; string lang; if (context.Request.IsAGetRequest() && TryGetDocumentsSubpathAndLanguage(context.Request, out subpath, out lang)) { bool isDirectory; string filePath = GetFilePath(subpath, out isDirectory); lang = string.IsNullOrEmpty(lang) ? _options.DefaultLanguage : lang; IDocumentHandler handler; var foundHandler = (lang != null && _documentHandlerResolver.TryResolveHandler(_options.FileProvider, filePath + $".{lang}", out handler)) || _documentHandlerResolver.TryResolveHandler(_options.FileProvider, filePath, out handler); if ((foundHandler || _options.DirectoryOptions.EnableDirectoryBrowsing) && isDirectory && !context.Request.Path.EndsInSlash()) { // If the path matches a directory but does not end in a slash, redirect to add the slash. // This prevents relative links from breaking. context.Response.StatusCode = 301; context.Response.Headers["Location"] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString; return; } if (!foundHandler) { if (isDirectory && _options.DirectoryOptions.EnableDirectoryBrowsing) { handler = new DirectoryHandler(_options.FileProvider, subpath, _options.DirectoryOptions); } else if (_options.EnableNotFoundHandling) { IFileInfo notFoundFile = null; if (lang != null) { _notFoundHtmlFilesPerLanguage.TryGetValue(lang, out notFoundFile); } handler = new NotFoundDocumentHandler(notFoundFile ?? _notFoundHtmlFile); } else { return; } } ApplyResponseHeaders(context.Response, handler); await ApplyResponseContent(context.Response, handler, lang); // Do not continue with the rest of middlewares return; } await _next(context); }
public async Task Invoke(HttpContext context) { PathString subpath; if (context.Request.IsAGetRequest() && TryGetDocumentsSubpath(context.Request, out subpath)) { bool isDirectory; string filePath = GetFilePath(subpath, out isDirectory); IDocumentHandler handler; var foundHandler = _documentHandlerResolver.TryResolveHandler(_options.FileProvider, filePath, out handler); if ((foundHandler || _options.DirectoryOptions.EnableDirectoryBrowsing) && isDirectory && !context.Request.Path.EndsInSlash()) { // If the path matches a directory but does not end in a slash, redirect to add the slash. // This prevents relative links from breaking. context.Response.StatusCode = 301; context.Response.Headers["Location"] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString; return; } if (!foundHandler) { if (isDirectory && _options.DirectoryOptions.EnableDirectoryBrowsing) { handler = new DirectoryHandler(_options.FileProvider, subpath, _options.DirectoryOptions); } else if (_options.EnableNotFoundHandling) { handler = new NotFoundDocumentHandler(_options.NotFoundHtmlFile); } else { return; } } ApplyResponseHeaders(context.Response, handler); await ApplyResponseContent(context.Response, handler); // Do not continue with the rest of middlewares return; } await _next(context); }