aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

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

Open John0King opened this issue 1 year ago • 0 comments

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<StaticFileOptions>, 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<IServiceProvider, IOptionsMonitor<StaticFileOptions>>)
  2. middleware use IOptionsMonitor<StaticFileOptions>>
  3. add an easy configuration structure
{
  "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

John0King avatar Aug 27 '24 10:08 John0King