Hosting #86 - Consume WebRootFileSystem
This commit is contained in:
parent
1bc4e21c22
commit
3343bb4d23
|
|
@ -6,12 +6,11 @@ namespace StaticFilesSample
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public void Configuration(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
app.UseFileServer(new FileServerOptions()
|
app.UseFileServer(new FileServerOptions()
|
||||||
{
|
{
|
||||||
EnableDirectoryBrowsing = true,
|
EnableDirectoryBrowsing = true,
|
||||||
FileSystem = new PhysicalFileSystem(@"c:\temp")
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
{
|
{
|
||||||
|
"commands": {
|
||||||
|
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.Urls http://localhost:12345/"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
|
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
|
||||||
|
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
|
||||||
"Kestrel": "1.0.0-*",
|
"Kestrel": "1.0.0-*",
|
||||||
"Microsoft.AspNet.StaticFiles": "1.0.0-*"
|
"Microsoft.AspNet.StaticFiles": "1.0.0-*"
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"aspnet50": { },
|
"aspnet50": { },
|
||||||
"aspnetcore50": {}
|
"aspnetcore50": { }
|
||||||
}
|
},
|
||||||
|
"webroot": "wwwroot"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
A static HTML file.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
public static class DefaultFilesExtensions
|
public static class DefaultFilesExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enables default file mapping on the current path from the current directory
|
/// Enables default file mapping on the current path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <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>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
public static IApplicationBuilder UseDefaultFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
/// <param name="options">The configuration options for this middleware.</param>
|
/// <param name="options">The configuration options for this middleware.</param>
|
||||||
public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options)
|
public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHostingEnvironment hostingEnv, [NotNull] DefaultFilesOptions options)
|
||||||
{
|
{
|
||||||
if (options.FileSystem == null)
|
options.ResolveFileSystem(hostingEnv);
|
||||||
{
|
|
||||||
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options;
|
_options = options;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
public static class DirectoryBrowserExtensions
|
public static class DirectoryBrowserExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enable directory browsing on the current path for the current directory
|
/// Enable directory browsing on the current path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <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>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
public static IApplicationBuilder UseDirectoryBrowser([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.Args_NoFormatter);
|
throw new ArgumentException(Resources.Args_NoFormatter);
|
||||||
}
|
}
|
||||||
if (options.FileSystem == null)
|
options.ResolveFileSystem(hostingEnv);
|
||||||
{
|
|
||||||
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options;
|
_options = options;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
public class DirectoryBrowserOptions : SharedOptionsBase<DirectoryBrowserOptions>
|
public class DirectoryBrowserOptions : SharedOptionsBase<DirectoryBrowserOptions>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enabled directory browsing in the current physical directory for all request paths
|
/// Enabled directory browsing for all request paths
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DirectoryBrowserOptions()
|
public DirectoryBrowserOptions()
|
||||||
: this(new SharedOptions())
|
: this(new SharedOptions())
|
||||||
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enabled directory browsing in the current physical directory for all request paths
|
/// Enabled directory browsing all request paths
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sharedOptions"></param>
|
/// <param name="sharedOptions"></param>
|
||||||
public DirectoryBrowserOptions(SharedOptions sharedOptions)
|
public DirectoryBrowserOptions(SharedOptions sharedOptions)
|
||||||
|
|
|
||||||
|
|
@ -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
|
/// Enables all static file middleware (except directory browsing) for the given request path from the directory of the same name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <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>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
public static IApplicationBuilder UseFileServer([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace Microsoft.AspNet.StaticFiles.Infrastructure
|
||||||
private PathString _requestPath;
|
private PathString _requestPath;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defaults to all request paths and the current physical directory.
|
/// Defaults to all request paths.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SharedOptions()
|
public SharedOptions()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNet.FileSystems;
|
using Microsoft.AspNet.FileSystems;
|
||||||
|
using Microsoft.AspNet.Hosting;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.StaticFiles.Infrastructure
|
namespace Microsoft.AspNet.StaticFiles.Infrastructure
|
||||||
|
|
@ -49,5 +50,17 @@ namespace Microsoft.AspNet.StaticFiles.Infrastructure
|
||||||
get { return SharedOptions.FileSystem; }
|
get { return SharedOptions.FileSystem; }
|
||||||
set { SharedOptions.FileSystem = value; }
|
set { SharedOptions.FileSystem = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void ResolveFileSystem(IHostingEnvironment hostingEnv)
|
||||||
|
{
|
||||||
|
if (FileSystem == null)
|
||||||
|
{
|
||||||
|
FileSystem = hostingEnv.WebRootFileSystem;
|
||||||
|
if (FileSystem == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Missing FileSystem.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
public static class StaticFileExtensions
|
public static class StaticFileExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enables static file serving for the current request path from the current directory
|
/// Enables static file serving for the current request path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
@ -22,10 +22,10 @@ namespace Microsoft.AspNet.Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <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>
|
/// <returns></returns>
|
||||||
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
public static IApplicationBuilder UseStaticFiles([NotNull] this IApplicationBuilder builder, [NotNull] string requestPath)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.Args_NoContentTypeProvider);
|
throw new ArgumentException(Resources.Args_NoContentTypeProvider);
|
||||||
}
|
}
|
||||||
if (options.FileSystem == null)
|
options.ResolveFileSystem(hostingEnv);
|
||||||
{
|
|
||||||
options.FileSystem = new PhysicalFileSystem(Helpers.ResolveRootPath(hostingEnv.WebRoot, options.RequestPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
_next = next;
|
_next = next;
|
||||||
_options = options;
|
_options = options;
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,14 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
public class StaticFileOptions : SharedOptionsBase<StaticFileOptions>
|
public class StaticFileOptions : SharedOptionsBase<StaticFileOptions>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defaults to all request paths in the current physical directory
|
/// Defaults to all request paths
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StaticFileOptions() : this(new SharedOptions())
|
public StaticFileOptions() : this(new SharedOptions())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defaults to all request paths in the current physical directory
|
/// Defaults to all request paths
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sharedOptions"></param>
|
/// <param name="sharedOptions"></param>
|
||||||
public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions)
|
public StaticFileOptions(SharedOptions sharedOptions) : base(sharedOptions)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
"version": "1.0.0-*",
|
"version": "1.0.0-*",
|
||||||
"description": "ASP.NET 5 static files middleware.",
|
"description": "ASP.NET 5 static files middleware.",
|
||||||
"dependencies": {
|
"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": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
||||||
"Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" }
|
"Microsoft.AspNet.HttpFeature": { "version": "1.0.0-*", "type": "build" }
|
||||||
|
|
|
||||||
|
|
@ -30,12 +30,6 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void GivenDirDoesntExist_Throw()
|
|
||||||
{
|
|
||||||
Assert.Throws<TargetInvocationException>(() => TestServer.Create(app => app.UseStaticFiles("/ThisDirDoesntExist")));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("", @".", "/missing.file")]
|
[InlineData("", @".", "/missing.file")]
|
||||||
[InlineData("/subdir", @".", "/subdir/missing.file")]
|
[InlineData("/subdir", @".", "/subdir/missing.file")]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue