You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/static-files.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,17 +58,18 @@ Creating performant web apps requires optimizing asset delivery to the browser.
58
58
59
59
The default web app templates call the <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> method in `Program.cs`, which enables static files to be served:
60
60
61
+
<!-- test via https://localhost:64889/test on the 2nd image -->
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`:
64
+
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`:
In the preceding markup, the tilde character `~` points to the [web root](xref:fundamentals/index#web-root).
70
71
71
-
### Serve files outside of web root
72
+
### Serve files outside of web root<!--move to static files-->
72
73
73
74
Consider a directory hierarchy in which the static files to be served reside outside of the [web root](xref:fundamentals/index#web-root):
74
75
@@ -87,20 +88,20 @@ A request can access the `red-rose.jpg` file by configuring the Static File Midd
87
88
In the preceding code, the *MyStaticFiles* directory hierarchy is exposed publicly via the *StaticFiles* URI segment. A request to `https://<hostname>/StaticFiles/images/red-rose.jpg` serves the `red-rose.jpg` file.
88
89
89
90
The following markup references `MyStaticFiles/images/red-rose.jpg`:
To serve files from multiple locations, see [Serve files from multiple locations](#serve-files-from-multiple-locations).
94
95
95
-
### Set HTTP response headers
96
+
### Set HTTP response headers<!--move to static files-->
96
97
97
98
A <xref:Microsoft.AspNetCore.Builder.StaticFileOptions> 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:
The preceding code makes static files publicly available in the local cache for one week.
102
103
103
-
## Static file authorization
104
+
## Static file authorization<!--move to static files-->
104
105
105
106
The ASP.NET Core templates call <xref:Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets%2A> before calling <xref:Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization%2A>. Most apps follow this pattern. When the Static File Middleware is called before the authorization middleware:
106
107
@@ -127,7 +128,6 @@ An alternative approach to serve files based on authorization is to:
127
128
* Store them outside of `wwwroot` and any directory accessible to the Static File Middleware.
128
129
* Serve them via an action method to which authorization is applied and return a <xref:Microsoft.AspNetCore.Mvc.FileResult> object:
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
138
138
139
139
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.
140
140
141
-
## Directory browsing
141
+
## Directory browsing<!--move to static files-->
142
142
143
143
Directory browsing allows directory listing within specified directories.
144
144
@@ -155,7 +155,7 @@ The preceding code allows directory browsing of the *wwwroot/images* folder usin
155
155
156
156
`AddDirectoryBrowser`[adds services](https://github.com/dotnet/aspnetcore/blob/fc4e391aa58a9fa67fdc3a96da6cfcadd0648b17/src/Middleware/StaticFiles/src/DirectoryBrowserServiceExtensions.cs#L25) required by the directory browsing middleware, including <xref:System.Text.Encodings.Web.HtmlEncoder>. These services may be added by other calls, such as <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddRazorPages%2A>, but we recommend calling `AddDirectoryBrowser` to ensure the services are added in all apps.
157
157
158
-
## Serve default documents
158
+
## Serve default documents<!--move to static files-->
159
159
160
160
<!-- Comment out @*@page*@ default RP file -->
161
161
@@ -178,7 +178,7 @@ The following code changes the default file name to `mydefault.html`:
### UseFileServer for default documents<!--move to static files-->
182
182
183
183
<xref:Microsoft.AspNetCore.Builder.FileServerExtensions.UseFileServer*> combines the functionality of `UseStaticFiles`, `UseDefaultFiles`, and optionally `UseDirectoryBrowser`.
184
184
@@ -229,12 +229,12 @@ If no default-named file exists in the *MyStaticFiles* directory, `https://<host
229
229
230
230
<xref:Microsoft.AspNetCore.Builder.DefaultFilesExtensions.UseDefaultFiles*> and <xref:Microsoft.AspNetCore.Builder.DirectoryBrowserExtensions.UseDirectoryBrowser*> perform a client-side redirect from the target URI without a trailing `/` to the target URI with a trailing `/`. For example, from `https://<hostname>/StaticFiles` to `https://<hostname>/StaticFiles/`. Relative URLs within the *StaticFiles* directory are invalid without a trailing slash (`/`) unless the <xref:Microsoft.AspNetCore.StaticFiles.Infrastructure.SharedOptions.RedirectToAppendTrailingSlash> option of <xref:Microsoft.AspNetCore.Builder.DefaultFilesOptions> is used.
231
231
232
-
## FileExtensionContentTypeProvider
232
+
## FileExtensionContentTypeProvider<!--move to static files-->
233
233
234
234
The <xref:Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider> 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:
235
235
236
236
<!-- test via /mapTest/image1.image and mapTest/test.htm3 /mapTest/TextFile.rtf -->
0 commit comments