Add OnPrepareResponse option w/resp header to static files doc (#2640)

* Create StartupAddHeader.cs

* Add files via upload

* Update static-files.md

* Update static-files.md

* Update StartupBrowse.cs

* Update StartupServeUnknownFileTypes.cs

* Update StartupStaticFiles.cs

* Update StartupTwoStaticFiles.cs

* Update StartupUseFileServer.cs

* Update static-files.md
pull/2649/head
Luke Latham 2017-02-01 18:26:26 -06:00 committed by Rick Anderson
parent 341f7e25a4
commit 8a10c9800d
8 changed files with 51 additions and 13 deletions

View File

@ -59,6 +59,12 @@ For a request to access *test.png*, configure the static files middleware as fol
A request to `http://<app>/StaticFiles/test.png` will serve the *test.png* file.
`StaticFileOptions()` can set response headers. For example, the code below sets up static file serving from the *wwwroot* folder and sets the `Cache-Control` header to make them publicly cacheable for 10 minutes (600 seconds):
[!code-csharp[Main](../fundamentals/static-files/sample/StartupAddHeader.cs?name=snippet1)]
![Response headers showing the Cache-Control header has been added](static-files/_static/add-header.png)
## Static file authorization
The static file module provides **no** authorization checks. Any files served by it, including those under *wwwroot* are publicly available. To serve files based on authorization:

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -0,0 +1,32 @@
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
namespace StaticFiles
{
public class StartupAddHeader
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=600");
}
});
}
#endregion
}
}

View File

@ -20,7 +20,7 @@ namespace StaticFiles
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
@ -40,4 +40,4 @@ namespace StaticFiles
}
#endregion
}
}
}

View File

@ -14,9 +14,9 @@ namespace StaticFiles
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions
app.UseStaticFiles(new StaticFileOptions()
{
ServeUnknownFileTypes = true,
DefaultContentType = "image/png"
@ -24,4 +24,4 @@ namespace StaticFiles
}
#endregion
}
}
}

View File

@ -17,10 +17,10 @@ namespace StaticFiles
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
}
#endregion
}
}
}

View File

@ -17,9 +17,9 @@ namespace StaticFiles
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions()
{
@ -30,4 +30,4 @@ namespace StaticFiles
}
#endregion
}
}
}

View File

@ -20,9 +20,9 @@ namespace StaticFiles
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
#region snippet1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseStaticFiles(); // For the wwwroot folder
app.UseFileServer(new FileServerOptions()
{
@ -34,4 +34,4 @@ namespace StaticFiles
}
#endregion
}
}
}