private static StaticFilesEndpointConventionBuilder MapStaticFilesCore(IEndpointRouteBuilder endpoints, PathString requestPath, StaticFileEndpointOptions options) { StaticFileOptions staticFileOptions; if (options == null) { var original = endpoints.ServiceProvider.GetRequiredService <IOptions <StaticFileOptions> >(); staticFileOptions = new StaticFileOptions() { ContentTypeProvider = original.Value.ContentTypeProvider, DefaultContentType = original.Value.DefaultContentType, FileProvider = original.Value.FileProvider, HttpsCompression = original.Value.HttpsCompression, OnPrepareResponse = original.Value.OnPrepareResponse, ServeUnknownFileTypes = original.Value.ServeUnknownFileTypes, }; } else { staticFileOptions = new StaticFileOptions() { ContentTypeProvider = options.ContentTypeProvider, DefaultContentType = options.DefaultContentType, FileProvider = options.FileProvider, HttpsCompression = options.HttpsCompression, OnPrepareResponse = options.OnPrepareResponse, ServeUnknownFileTypes = options.ServeUnknownFileTypes, }; } staticFileOptions.ContentTypeProvider ??= new FileExtensionContentTypeProvider(); staticFileOptions.FileProvider ??= endpoints.ServiceProvider.GetRequiredService <IWebHostEnvironment>().WebRootFileProvider; staticFileOptions.RequestPath = requestPath; var pattern = RoutePatternFactory.Parse(requestPath + "/{**path}", defaults: null, parameterPolicies: new { path = new FileExistsConstraint(options.FileProvider), }); var app = endpoints.CreateApplicationBuilder(); // Temporary hack app.Use(next => httpContext => { httpContext.SetEndpoint(null); return(next(httpContext)); }); app.UseStaticFiles(staticFileOptions); return(new StaticFilesEndpointConventionBuilder(endpoints.Map(pattern, app.Build()))); }
public static StaticFilesEndpointConventionBuilder MapStaticFiles(this IEndpointRouteBuilder endpoints, PathString requestPath, StaticFileEndpointOptions options) { if (endpoints == null) { throw new ArgumentNullException(nameof(endpoints)); } return(MapStaticFilesCore(endpoints, requestPath, options)); }