private bool ShouldServeThisFile( IOwinContext context, out StaticFileContext fileContext) { // Capture the configuration because it can change at any time fileContext = new StaticFileContext { Configuration = _configuration, RootFolder = _rootFolder, RootUrl = _rootUrl }; if (!string.Equals(context.Request.Method, "GET", StringComparison.OrdinalIgnoreCase)) { _traceFilter.Trace(context, TraceLevel.Debug, () => GetType().Name + " only handles GET requests"); return(false); } var request = context.Request; if (!_configuration.Enabled) { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " is disabled and will not serve this request"); return(false); } if (!request.Path.HasValue) { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " can not serve this request because the request path is empty"); return(false); } if (!fileContext.RootUrl.HasValue) { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " will not handle this request because the configured root URL is empty"); return(false); } if (!(fileContext.RootUrl.Value == "/" || request.Path.StartsWithSegments(fileContext.RootUrl))) { _traceFilter.Trace(context, TraceLevel.Debug, () => GetType().Name + " will not handle this request because the file is not in a sub-directory of the configured root"); return(false); } // Extract the path relative to the root UI var relativePath = request.Path.Value.Substring(fileContext.RootUrl.Value.Length); var fileName = relativePath.Replace('/', '\\'); // No filename case can't be handled by this middleware if (string.IsNullOrWhiteSpace(fileName) || fileName == "\\") { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " will not handle this request because the file name is blank"); return(false); } if (fileName.StartsWith("\\")) { fileName = fileName.Substring(1); } if (!fileContext.Configuration.IncludeSubFolders && fileName.Contains("\\")) { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " will not handle this request because it is configured to not serve files from sub-folders"); return(false); } // Parse out pieces of the file name var lastPeriodIndex = fileName.LastIndexOf('.'); var extension = lastPeriodIndex < 0 ? "" : fileName.Substring(lastPeriodIndex); // Get the configuration appropriate to this file extension fileContext.ExtensionConfiguration = fileContext.Configuration.FileExtensions .FirstOrDefault(c => string.Equals(c.Extension, extension, StringComparison.OrdinalIgnoreCase)); // Only serve files that have their extensions confgured if (fileContext.ExtensionConfiguration == null) { _traceFilter.Trace(context, TraceLevel.Error, () => GetType().Name + " will not handle this request because " + extension + " file extensions are not configured"); return(false); } fileContext.PhysicalFile = new FileInfo(Path.Combine(fileContext.RootFolder, fileName)); var file = fileContext.PhysicalFile; fileContext.FileExists = file.Exists; if (fileContext.FileExists) { _traceFilter.Trace(context, TraceLevel.Information, () => GetType().Name + " this is a request for static file " + file); } else { _traceFilter.Trace(context, TraceLevel.Information, () => GetType().Name + " static file '" + file + "' does not exist on disk"); } return(fileContext.FileExists); }