dotnet / dotnet/aspnetcore

allow configure static files with `IOptions<T>` and `IConfiguration`

Open
#57,544 0 comments 0 reactions 0 assignees View on GitHub
area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

when I create a proxy service with `YARP` , I need to serve static files like nginx, but it need to be able to configure mime type mapping throw `appsettings.json`.

https://github.com/dotnet/aspnetcore/blob/369b2dedc04c58d20322a71d2d8eb255615d5757/src/Middleware/StaticFiles/src/StaticFileExtensions.cs#L25-L30

the "default" `UseStaticFile` indeed use `IOptions`, but it is a `Singleton` service injected to `constructor`
and it can not to configure it through `appsettings.json`

### Describe the solution you'd like

1. use a new base method `UseStaticFile(this IApplicationBuilder app,Func>)`
2. middleware use `IOptionsMonitor>`
3. add an easy configuration structure

```jsonc
{
"StaticFile": {
"AllowDefaultMappings": true, // default to true, false will clear all default mime mappings
"Mappings" : {
".apk":"application/vnd.android.package-archive",
".xxx":"yyyyyyyy"
},
"ServeUnknownFileTypes":false,
"RedirectToAppendTrailingSlash":fase,
"DefaultFileNames":[ // this is for configure UseDefaultFiles
"index.html",
"index.htm"
]
}
}
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.