diff --git a/samples/StaticFileSample/Startup.cs b/samples/StaticFileSample/Startup.cs index 15ebd62693..a6337fe7d6 100644 --- a/samples/StaticFileSample/Startup.cs +++ b/samples/StaticFileSample/Startup.cs @@ -6,12 +6,11 @@ namespace StaticFilesSample { public class Startup { - public void Configuration(IApplicationBuilder app) + public void Configure(IApplicationBuilder app) { app.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = true, - FileSystem = new PhysicalFileSystem(@"c:\temp") }); } } diff --git a/samples/StaticFileSample/project.json b/samples/StaticFileSample/project.json index f0995a4731..0b07dc6047 100644 --- a/samples/StaticFileSample/project.json +++ b/samples/StaticFileSample/project.json @@ -1,11 +1,16 @@ { + "commands": { + "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.Urls http://localhost:12345/" + }, "dependencies": { "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Kestrel": "1.0.0-*", "Microsoft.AspNet.StaticFiles": "1.0.0-*" }, "frameworks": { "aspnet50": { }, - "aspnetcore50": {} - } + "aspnetcore50": { } + }, + "webroot": "wwwroot" } diff --git a/samples/StaticFileSample/wwwroot/htmlpage.html b/samples/StaticFileSample/wwwroot/htmlpage.html new file mode 100644 index 0000000000..c2dacddcb9 --- /dev/null +++ b/samples/StaticFileSample/wwwroot/htmlpage.html @@ -0,0 +1,11 @@ + + + + + + + + + A static HTML file. + + \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs b/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs index 0ea6c3a8f4..9f974268e8 100644 --- a/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/DefaultFilesExtensions.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder public static class DefaultFilesExtensions { /// - /// Enables default file mapping on the current path from the current directory + /// Enables default file mapping on the current path /// /// /// @@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder } /// - /// Enables default file mapping for the given request path from the directory of the same name + /// Enables default file mapping for the given request path /// /// - /// The relative request path and physical path. + /// The relative request path. /// public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { diff --git a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs index 6373705ca9..787cc0421e 100644 --- a/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs @@ -29,10 +29,7 @@ namespace Microsoft.AspNet.StaticFiles /// The configuration options for this middleware. public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options) { - if (options.FileSystem == null) - { - options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); - } + options.ResolveFileSystem(hostingEnv); _next = next; _options = options; diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs index fe32f39028..8d3a6af9f7 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserExtensions.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder public static class DirectoryBrowserExtensions { /// - /// Enable directory browsing on the current path for the current directory + /// Enable directory browsing on the current path /// /// /// @@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder } /// - /// Enables directory browsing for the given request path from the directory of the same name + /// Enables directory browsing for the given request path /// /// - /// The relative request path and physical path. + /// The relative request path. /// public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs index 8b444538ef..596dbf094e 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs @@ -31,10 +31,7 @@ namespace Microsoft.AspNet.StaticFiles { throw new ArgumentException(Resources.Args_NoFormatter); } - if (options.FileSystem == null) - { - options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); - } + options.ResolveFileSystem(hostingEnv); _next = next; _options = options; diff --git a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserOptions.cs b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserOptions.cs index 95858311a3..8a22467944 100644 --- a/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserOptions.cs +++ b/src/Microsoft.AspNet.StaticFiles/DirectoryBrowserOptions.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.StaticFiles public class DirectoryBrowserOptions : SharedOptionsBase { /// - /// Enabled directory browsing in the current physical directory for all request paths + /// Enabled directory browsing for all request paths /// public DirectoryBrowserOptions() : this(new SharedOptions()) @@ -19,7 +19,7 @@ namespace Microsoft.AspNet.StaticFiles } /// - /// Enabled directory browsing in the current physical directory for all request paths + /// Enabled directory browsing all request paths /// /// public DirectoryBrowserOptions(SharedOptions sharedOptions) diff --git a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs index 9de67fc531..250fa60d6d 100644 --- a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Builder /// 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. + /// The relative request path. /// public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { diff --git a/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs b/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs deleted file mode 100644 index d510d0ba23..0000000000 --- a/src/Microsoft.AspNet.StaticFiles/IHostingEnvironment.cs +++ /dev/null @@ -1,15 +0,0 @@ -// 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 Microsoft.Framework.Runtime; - -namespace Microsoft.AspNet.Hosting -{ - [AssemblyNeutral] - public interface IHostingEnvironment - { - string EnvironmentName { get; } - - string WebRoot { get; } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptions.cs b/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptions.cs index 027cd02e06..936ae0f534 100644 --- a/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptions.cs +++ b/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptions.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNet.StaticFiles.Infrastructure private PathString _requestPath; /// - /// Defaults to all request paths and the current physical directory. + /// Defaults to all request paths. /// public SharedOptions() { diff --git a/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptionsBase.cs b/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptionsBase.cs index 7c790db365..27f69fb0e8 100644 --- a/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptionsBase.cs +++ b/src/Microsoft.AspNet.StaticFiles/Infrastructure/SharedOptionsBase.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.StaticFiles.Infrastructure @@ -49,5 +50,17 @@ namespace Microsoft.AspNet.StaticFiles.Infrastructure get { return SharedOptions.FileSystem; } set { SharedOptions.FileSystem = value; } } + + internal void ResolveFileSystem(IHostingEnvironment hostingEnv) + { + if (FileSystem == null) + { + FileSystem = hostingEnv.WebRootFileSystem; + if (FileSystem == null) + { + throw new InvalidOperationException("Missing FileSystem."); + } + } + } } } diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs index 0a95e1f36e..b8fcfc2d4f 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileExtensions.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder public static class StaticFileExtensions { /// - /// Enables static file serving for the current request path from the current directory + /// Enables static file serving for the current request path /// /// /// @@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder } /// - /// Enables static file serving for the given request path from the directory of the same name + /// Enables static file serving for the given request path /// /// - /// The relative request path and physical path. + /// The relative request path. /// public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath) { diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs index e55ae49e6a..a630f367f2 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs @@ -30,10 +30,7 @@ namespace Microsoft.AspNet.StaticFiles { throw new ArgumentException(Resources.Args_NoContentTypeProvider); } - if (options.FileSystem == null) - { - options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath)); - } + options.ResolveFileSystem(hostingEnv); _next = next; _options = options; diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileOptions.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileOptions.cs index e93b6f2a6d..81624a81ef 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileOptions.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileOptions.cs @@ -12,14 +12,14 @@ namespace Microsoft.AspNet.StaticFiles public class StaticFileOptions : SharedOptionsBase { /// - /// Defaults to all request paths in the current physical directory + /// Defaults to all request paths /// public StaticFileOptions() : this(new SharedOptions()) { } /// - /// Defaults to all request paths in the current physical directory + /// Defaults to all request paths /// /// public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions) diff --git a/src/Microsoft.AspNet.StaticFiles/project.json b/src/Microsoft.AspNet.StaticFiles/project.json index f9ffe23d9a..32b6764bb5 100644 --- a/src/Microsoft.AspNet.StaticFiles/project.json +++ b/src/Microsoft.AspNet.StaticFiles/project.json @@ -2,7 +2,8 @@ "version": "1.0.0-*", "description": "ASP.NET 5 static files middleware.", "dependencies": { - "Microsoft.AspNet.FileSystems": "1.0.0-*", + "Microsoft.AspNet.FileSystems.Interfaces": { "version": "1.0.0-*", "type": "build" }, + "Microsoft.AspNet.Hosting": { "version": "1.0.0-*", "type": "build" }, "Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Http.Extensions": "1.0.0-*", "Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" } diff --git a/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs index 51f754633f..07106e6bbf 100644 --- a/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs +++ b/test/Microsoft.AspNet.StaticFiles.Tests/StaticFileMiddlewareTests.cs @@ -30,12 +30,6 @@ namespace Microsoft.AspNet.StaticFiles Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } - [Fact] - public void GivenDirDoesntExist_Throw() - { - Assert.Throws(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist"))); - } - [Theory] [InlineData("", @".", "/missing.file")] [InlineData("/subdir", @".", "/subdir/missing.file")]