// Copyright (c) Microsoft Open Technologies, Inc. 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.StaticFiles; namespace Microsoft.AspNet.Builder { /// /// Extension methods that combine all of the static file middleware components: /// Default files, directory browsing, send file, and static files /// public static class FileServerExtensions { /// /// Enable all static file middleware (except directory browsing) for the current request path in the current directory. /// /// /// public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder) { return builder.UseFileServer(new FileServerOptions()); } /// /// Enable all static file middleware on for the current request path in the current directory. /// /// /// Should directory browsing be enabled? /// public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, bool enableDirectoryBrowsing) { return builder.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = enableDirectoryBrowsing }); } /// /// Enables all static file middleware (except directory browsing) for the given request path from the directory of the same name /// /// /// The relative request path and physical path. /// public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { return builder.UseFileServer(new FileServerOptions() { RequestPath = new PathString(requestPath) }); } /// /// Enable all static file middleware with the given options /// /// /// /// public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] FileServerOptions options) { if (options == null) { throw new ArgumentNullException("options"); } if (options.EnableDefaultFiles) { builder = builder.UseDefaultFiles(options.DefaultFilesOptions); } if (options.EnableDirectoryBrowsing) { builder = builder.UseDirectoryBrowser(options.DirectoryBrowserOptions); } return builder .UseSendFileFallback() .UseStaticFiles(options.StaticFileOptions); } } }