Skip to content

Commit ad4a2ca

Browse files
committed
proto of moving mapSaticAssets
1 parent 525450c commit ad4a2ca

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

aspnetcore/fundamentals/static-files.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ Creating performant web apps requires optimizing asset delivery to the browser.
5858

5959
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:
6060

61+
<!-- test via https://localhost:64889/test on the 2nd image -->
6162
[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet&highlight=15)]
6263

63-
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`:
6465

6566
```html
6667
<img src="~/images/MyImage.jpg" class="img" alt="My image" />
6768
```
6869

6970
In the preceding markup, the tilde character `~` points to the [web root](xref:fundamentals/index#web-root).
7071

71-
### Serve files outside of web root
72+
### Serve files outside of web root <!--move to static files-->
7273

7374
Consider a directory hierarchy in which the static files to be served reside outside of the [web root](xref:fundamentals/index#web-root):
7475

@@ -87,20 +88,20 @@ A request can access the `red-rose.jpg` file by configuring the Static File Midd
8788
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.
8889

8990
The following markup references `MyStaticFiles/images/red-rose.jpg`:
90-
<!-- zz test via /Home2/MyStaticFilesRR -->
91+
<!-- test via /Home2/MyStaticFilesRR -->
9192
[!code-html[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Views/Home2/MyStaticFilesRR.cshtml?range=1)]
9293

9394
To serve files from multiple locations, see [Serve files from multiple locations](#serve-files-from-multiple-locations).
9495

95-
### Set HTTP response headers
96+
### Set HTTP response headers <!--move to static files-->
9697

9798
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:
9899

99100
[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_rh&highlight=16-24)]
100101

101102
The preceding code makes static files publicly available in the local cache for one week.
102103

103-
## Static file authorization
104+
## Static file authorization <!--move to static files-->
104105

105106
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:
106107

@@ -127,7 +128,6 @@ An alternative approach to serve files based on authorization is to:
127128
* Store them outside of `wwwroot` and any directory accessible to the Static File Middleware.
128129
* Serve them via an action method to which authorization is applied and return a <xref:Microsoft.AspNetCore.Mvc.FileResult> object:
129130

130-
131131
[!code-csharp[](~/fundamentals/static-files/samples/6.x/StaticFileAuth/Pages/BannerImage.cshtml.cs?name=snippet)]
132132

133133
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
138138

139139
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.
140140

141-
## Directory browsing
141+
## Directory browsing <!--move to static files-->
142142

143143
Directory browsing allows directory listing within specified directories.
144144

@@ -155,7 +155,7 @@ The preceding code allows directory browsing of the *wwwroot/images* folder usin
155155

156156
`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.
157157

158-
## Serve default documents
158+
## Serve default documents <!--move to static files-->
159159

160160
<!-- Comment out @*@page*@ default RP file -->
161161

@@ -178,7 +178,7 @@ The following code changes the default file name to `mydefault.html`:
178178

179179
[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_df2&highlight=16-19)]
180180

181-
### UseFileServer for default documents
181+
### UseFileServer for default documents <!--move to static files-->
182182

183183
<xref:Microsoft.AspNetCore.Builder.FileServerExtensions.UseFileServer*> combines the functionality of `UseStaticFiles`, `UseDefaultFiles`, and optionally `UseDirectoryBrowser`.
184184

@@ -229,12 +229,12 @@ If no default-named file exists in the *MyStaticFiles* directory, `https://<host
229229

230230
<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.
231231

232-
## FileExtensionContentTypeProvider
232+
## FileExtensionContentTypeProvider <!--move to static files-->
233233

234234
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:
235235

236236
<!-- test via /mapTest/image1.image and mapTest/test.htm3 /mapTest/TextFile.rtf -->
237-
[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_fec&highlight=19-33)]
237+
[!code-csharp[](~/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs?name=snippet_fec&highlight=19-33)]
238238

239239
See [MIME content types](https://www.iana.org/assignments/media-types/media-types.xhtml).
240240

aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Pages/Test.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
<p> Test /MyStaticFiles/image3.png</p>
44

55
<img src="~/image3.png" class="img" asp-append-version="true" alt="Test">
6+
7+
8+
<p> Test ~/images/MyImage.jpg</p>
9+
10+
<img src="~/images/MyImage.jpg" class="img" asp-append-version="true" alt="Test">

aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define TREE // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS MUL MULT2
1+
#define DEFAULT // DEFAULT RR RH DB DF DF2 UFS UFS2 TREE FECTP NS MUL MULT2
22
// Test1
33
#if NEVER
44
#elif DEFAULT

0 commit comments

Comments
 (0)