Hosting #86 - Consume WebRootFileSystem

This commit is contained in:
Chris Ross 2014-12-05 11:03:19 -08:00
parent 1bc4e21c22
commit 3343bb4d23
17 changed files with 52 additions and 53 deletions

View File

@ -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")
});
}
}

View File

@ -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"
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
A static HTML file.
</body>
</html>

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
public static class DefaultFilesExtensions
{
/// <summary>
/// Enables default file mapping on the current path from the current directory
/// Enables default file mapping on the current path
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
}
/// <summary>
/// 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
/// </summary>
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{

View File

@ -29,10 +29,7 @@ namespace Microsoft.AspNet.StaticFiles
/// <param name="options">The configuration options for this middleware.</param>
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;

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
public static class DirectoryBrowserExtensions
{
/// <summary>
/// Enable directory browsing on the current path for the current directory
/// Enable directory browsing on the current path
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
}
/// <summary>
/// Enables directory browsing for the given request path from the directory of the same name
/// Enables directory browsing for the given request path
/// </summary>
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{

View File

@ -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;

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNet.StaticFiles
public class DirectoryBrowserOptions : SharedOptionsBase<DirectoryBrowserOptions>
{
/// <summary>
/// Enabled directory browsing in the current physical directory for all request paths
/// Enabled directory browsing for all request paths
/// </summary>
public DirectoryBrowserOptions()
: this(new SharedOptions())
@ -19,7 +19,7 @@ namespace Microsoft.AspNet.StaticFiles
}
/// <summary>
/// Enabled directory browsing in the current physical directory for all request paths
/// Enabled directory browsing all request paths
/// </summary>
/// <param name="sharedOptions"></param>
public DirectoryBrowserOptions(SharedOptions sharedOptions)

View File

@ -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
/// </summary>
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{

View File

@ -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; }
}
}

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNet.StaticFiles.Infrastructure
private PathString _requestPath;
/// <summary>
/// Defaults to all request paths and the current physical directory.
/// Defaults to all request paths.
/// </summary>
public SharedOptions()
{

View File

@ -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.");
}
}
}
}
}

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
public static class StaticFileExtensions
{
/// <summary>
/// Enables static file serving for the current request path from the current directory
/// Enables static file serving for the current request path
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
}
/// <summary>
/// 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
/// </summary>
/// <param name="builder"></param>
/// <param name="requestPath">The relative request path and physical path.</param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
{

View File

@ -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;

View File

@ -12,14 +12,14 @@ namespace Microsoft.AspNet.StaticFiles
public class StaticFileOptions : SharedOptionsBase<StaticFileOptions>
{
/// <summary>
/// Defaults to all request paths in the current physical directory
/// Defaults to all request paths
/// </summary>
public StaticFileOptions() : this(new SharedOptions())
{
}
/// <summary>
/// Defaults to all request paths in the current physical directory
/// Defaults to all request paths
/// </summary>
/// <param name="sharedOptions"></param>
public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions)

View File

@ -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" }

View File

@ -30,12 +30,6 @@ namespace Microsoft.AspNet.StaticFiles
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public void GivenDirDoesntExist_Throw()
{
Assert.Throws<TargetInvocationException>(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist")));
}
[Theory]
[InlineData("", @".", "/missing.file")]
[InlineData("/subdir", @".", "/subdir/missing.file")]