-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
DefaultFilesExtensions.cs
65 lines (57 loc) · 2.29 KB
/
DefaultFilesExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder;
/// <summary>
/// Extension methods for the DefaultFilesMiddleware
/// </summary>
public static class DefaultFilesExtensions
{
/// <summary>
/// Enables default file mapping on the current path
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
/// <remarks>
/// Files are served from the path specified in <see cref="IWebHostEnvironment.WebRootPath"/>
/// or <see cref="IWebHostEnvironment.WebRootFileProvider"/> which defaults to the 'wwwroot' subfolder.
/// </remarks>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
return app.UseMiddleware<DefaultFilesMiddleware>();
}
/// <summary>
/// Enables default file mapping for the given request path
/// </summary>
/// <param name="app"></param>
/// <param name="requestPath">The relative request path.</param>
/// <returns></returns>
/// <remarks>
/// Files are served from the path specified in <see cref="IWebHostEnvironment.WebRootPath"/>
/// or <see cref="IWebHostEnvironment.WebRootFileProvider"/> which defaults to the 'wwwroot' subfolder.
/// </remarks>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder app, string requestPath)
{
ArgumentNullException.ThrowIfNull(app);
return app.UseDefaultFiles(new DefaultFilesOptions
{
RequestPath = new PathString(requestPath)
});
}
/// <summary>
/// Enables default file mapping with the given options
/// </summary>
/// <param name="app"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseDefaultFiles(this IApplicationBuilder app, DefaultFilesOptions options)
{
ArgumentNullException.ThrowIfNull(app);
ArgumentNullException.ThrowIfNull(options);
return app.UseMiddleware<DefaultFilesMiddleware>(Options.Create(options));
}
}