diff --git a/aspnetcore/fundamentals/static-files.md b/aspnetcore/fundamentals/static-files.md index 79a0f98ecfbe..d3819228626a 100644 --- a/aspnetcore/fundamentals/static-files.md +++ b/aspnetcore/fundamentals/static-files.md @@ -58,9 +58,10 @@ Creating performant web apps requires optimizing asset delivery to the browser. The default web app templates call the method in `Program.cs`, which enables static files to be served: + [!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet&highlight=15)] -The parameterless `UseStaticFiles` method overload marks the files in [web root](xref:fundamentals/index#web-root) as servable. The following markup references `wwwroot/images/MyImage.jpg`: +The `MapStaticAssets` method overload marks the files in [web root](xref:fundamentals/index#web-root) as servable. The following markup references `wwwroot/images/MyImage.jpg`: ```html My image @@ -68,7 +69,7 @@ The parameterless `UseStaticFiles` method overload marks the files in [web root] In the preceding markup, the tilde character `~` points to the [web root](xref:fundamentals/index#web-root). -### Serve files outside of web root +### Serve files outside of web root Consider a directory hierarchy in which the static files to be served reside outside of the [web root](xref:fundamentals/index#web-root): @@ -87,12 +88,12 @@ A request can access the `red-rose.jpg` file by configuring the Static File Midd In the preceding code, the *MyStaticFiles* directory hierarchy is exposed publicly via the *StaticFiles* URI segment. A request to `https:///StaticFiles/images/red-rose.jpg` serves the `red-rose.jpg` file. The following markup references `MyStaticFiles/images/red-rose.jpg`: - + [!code-html[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Views/Home2/MyStaticFilesRR.cshtml?range=1)] To serve files from multiple locations, see [Serve files from multiple locations](#serve-files-from-multiple-locations). -### Set HTTP response headers +### Set HTTP response headers A object can be used to set HTTP response headers. In addition to configuring static file serving from the [web root](xref:fundamentals/index#web-root), the following code sets the [Cache-Control](https://developer.mozilla.org/docs/Web/HTTP/Headers/Cache-Control) header: @@ -100,7 +101,7 @@ A object can be used to se The preceding code makes static files publicly available in the local cache for one week. -## Static file authorization +## Static file authorization The ASP.NET Core templates call before calling . Most apps follow this pattern. When the Static File Middleware is called before the authorization middleware: @@ -127,7 +128,6 @@ An alternative approach to serve files based on authorization is to: * Store them outside of `wwwroot` and any directory accessible to the Static File Middleware. * Serve them via an action method to which authorization is applied and return a object: - [!code-csharp[](~/fundamentals/static-files/samples/6.x/StaticFileAuth/Pages/BannerImage.cshtml.cs?name=snippet)] The preceding approach requires a page or endpoint per file. The following code returns files or uploads files for authenticated users: @@ -138,7 +138,7 @@ IFormFile in the preceding sample uses memory buffer for uploading. For handling See the [StaticFileAuth](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/static-files/samples/9.x/StaticFileAuth) GitHub folder for the complete sample. -## Directory browsing +## Directory browsing Directory browsing allows directory listing within specified directories. @@ -155,7 +155,7 @@ The preceding code allows directory browsing of the *wwwroot/images* folder usin `AddDirectoryBrowser` [adds services](https://github.com/dotnet/aspnetcore/blob/fc4e391aa58a9fa67fdc3a96da6cfcadd0648b17/src/Middleware/StaticFiles/src/DirectoryBrowserServiceExtensions.cs#L25) required by the directory browsing middleware, including . These services may be added by other calls, such as , but we recommend calling `AddDirectoryBrowser` to ensure the services are added in all apps. -## Serve default documents +## Serve default documents @@ -178,7 +178,7 @@ The following code changes the default file name to `mydefault.html`: [!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_df2&highlight=16-19)] -### UseFileServer for default documents +### UseFileServer for default documents combines the functionality of `UseStaticFiles`, `UseDefaultFiles`, and optionally `UseDirectoryBrowser`. @@ -229,12 +229,12 @@ If no default-named file exists in the *MyStaticFiles* directory, `https:// and perform a client-side redirect from the target URI without a trailing `/` to the target URI with a trailing `/`. For example, from `https:///StaticFiles` to `https:///StaticFiles/`. Relative URLs within the *StaticFiles* directory are invalid without a trailing slash (`/`) unless the option of is used. -## FileExtensionContentTypeProvider +## FileExtensionContentTypeProvider The class contains a [Mappings](/dotnet/api/microsoft.aspnetcore.staticfiles.fileextensioncontenttypeprovider.mappings) property that serves as a mapping of file extensions to MIME content types. In the following sample, several file extensions are mapped to known MIME types. The *.rtf* extension is replaced, and *.mp4* is removed: -[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_fec&highlight=19-33)] +[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_fec&highlight=19-33)] See [MIME content types](https://www.iana.org/assignments/media-types/media-types.xhtml). diff --git a/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Pages/Test.cshtml b/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Pages/Test.cshtml index 865b9c1d616a..d7079c1df1e8 100644 --- a/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Pages/Test.cshtml +++ b/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Pages/Test.cshtml @@ -3,3 +3,8 @@

Test /MyStaticFiles/image3.png

Test + + +

Test ~/images/MyImage.jpg

+ +Test diff --git a/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs b/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs index 6bb507555e82..d93be9803196 100644 --- a/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs +++ b/aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs @@ -1,4 +1,4 @@ -#define TREE // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS MUL MULT2 +#define DEFAULT // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS MUL MULT2 // Test1 #if NEVER #elif DEFAULT