我的Web应用程序需要让管理员用户从.net core 2 app添加和删除服务文件夹。 我找到了一种提供服务文件夹列表的方法,但是一旦配置了应用程序,我找不到动态添加或删除它们的方法。
如何从应用程序中重新运行configure函数? 或者,如何在已运行的服务中添加或删除UseFileServer()配置?
public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseDeveloperExceptionPage(); app.UseMvc(); //get these dynamically from the database var locations = new Dictionary<string, string>{ {@"C:\folder1", "/folder1"}, {@"D:\folder2", "/folder2"} }; foreach (var kvp in locations) { app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider( kvp.Key ), RequestPath = new PathString(kvp.Value), EnableDirectoryBrowsing = true }); } } }我正在使用.net core 2.0.0-preview2-final。
My web application needs to let an admin user add and remove served folders from a .net core 2 app. I have found a way to provide a list of served folders, but I can't find a way to dynamically add or remove them once the app has been configured.
How do I re-run the configure function from within the application? Alternatively, how do I add or remove UseFileServer() configurations within an already-running service?
public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseDeveloperExceptionPage(); app.UseMvc(); //get these dynamically from the database var locations = new Dictionary<string, string>{ {@"C:\folder1", "/folder1"}, {@"D:\folder2", "/folder2"} }; foreach (var kvp in locations) { app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider( kvp.Key ), RequestPath = new PathString(kvp.Value), EnableDirectoryBrowsing = true }); } } }I'm using .net core 2.0.0-preview2-final.
最满意答案
您可能希望根据您的设置动态注入FileServer中间件。
微软的Chris Ross'Github上有一个示例项目: https : //github.com/Tratcher/MiddlewareInjector/tree/master/MiddlewareInjector
您必须将上述repo中的MiddlewareInjectorOptions , MiddlewareInjectorMiddleware和MiddlewareInjectorExtensions类添加到您的项目中。
然后,在Startup类中,将MiddlewareInjectorOptions注册为单例(因此它在整个应用程序中可用)并使用MiddlewareInjector:
public class Startup { public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddSingleton<MiddlewareInjectorOptions>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); var injectorOptions = app.ApplicationServices.GetService<MiddlewareInjectorOptions>(); app.UseMiddlewareInjector(injectorOptions); app.UseWelcomePage(); } }然后,在任何地方注入MiddlewareInjectorOptions并动态配置中间件,如下所示:
public class FileServerConfigurator { private readonly MiddlewareInjectorOptions middlewareInjectorOptions; public FileServerConfigurator(MiddlewareInjectorOptions middlewareInjectorOptions) { this.middlewareInjectorOptions = middlewareInjectorOptions; } public void SetPath(string requestPath, string physicalPath) { var fileProvider = new PhysicalFileProvider(physicalPath); middlewareInjectorOptions.InjectMiddleware(app => { app.UseFileServer(new FileServerOptions { RequestPath = requestPath, FileProvider = fileProvider, EnableDirectoryBrowsing = true }); }); } }请注意,此MiddlewareInjector只能注入一个中间件,因此您的代码应为您要服务的每个路径调用UseFileServer() 。
我用所需的代码创建了一个Gist: https : //gist.github.com/michaldudak/4eb6b0b26405543cff4c4f01a51ea869
You may want to dynamically inject the FileServer middleware based on your settings.
There is an example project on Microsoft's Chris Ross' Github: https://github.com/Tratcher/MiddlewareInjector/tree/master/MiddlewareInjector
You'll have to add the MiddlewareInjectorOptions, MiddlewareInjectorMiddleware and MiddlewareInjectorExtensions classes from the aforementioned repo to your project.
Then, in your Startup class, register the MiddlewareInjectorOptions as a singleton (so it's available throughout your application) and use the MiddlewareInjector:
public class Startup { public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddSingleton<MiddlewareInjectorOptions>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); var injectorOptions = app.ApplicationServices.GetService<MiddlewareInjectorOptions>(); app.UseMiddlewareInjector(injectorOptions); app.UseWelcomePage(); } }Then, inject the MiddlewareInjectorOptions wherever you want and configure the middleware dynamically, like this:
public class FileServerConfigurator { private readonly MiddlewareInjectorOptions middlewareInjectorOptions; public FileServerConfigurator(MiddlewareInjectorOptions middlewareInjectorOptions) { this.middlewareInjectorOptions = middlewareInjectorOptions; } public void SetPath(string requestPath, string physicalPath) { var fileProvider = new PhysicalFileProvider(physicalPath); middlewareInjectorOptions.InjectMiddleware(app => { app.UseFileServer(new FileServerOptions { RequestPath = requestPath, FileProvider = fileProvider, EnableDirectoryBrowsing = true }); }); } }Note that this MiddlewareInjector can inject just a single middleware, so your code should call UseFileServer() for each path you want to serve.
I've created a Gist with the required code: https://gist.github.com/michaldudak/4eb6b0b26405543cff4c4f01a51ea869
更多推荐
发布评论