启动后添加新的FileServer位置(启动后编辑中间件)(Add new FileServer locations after startup (edit middleware after star

编程入门 行业动态 更新时间:2024-10-27 00:24:43
启动后添加新的FileServer位置(启动后编辑中间件)(Add new FileServer locations after startup (edit middleware after startup))

我的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

更多推荐

本文发布于:2023-08-02 18:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中间件   位置   编辑   FileServer   Add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!