【问题标题】:MVC Core 2.1 Directory Browser some files not shownMVC Core 2.1 目录浏览器一些文件未显示
【发布时间】:2019-11-25 11:19:16
【问题描述】:

我有一个 MVC 核心应用程序,其中包含一些允许浏览的文件夹。 我使用 UseDirectoryBrowser 来映射共享文件夹,它们似乎是可浏览的,问题是并非所有文件都显示(特别是具有 MSI 扩展名的文件)。 这些文件在首次上传时确实会出现,但在刷新页面后会消失。 我们最近迁移到 azure,之前在不同的托管服务提供商上使用了相同的代码,而我们从未遇到过同样的问题。

有什么我可以研究的吗?

【问题讨论】:

    标签: azure asp.net-core-mvc


    【解决方案1】:

    我无法重现您的问题,但这是我在我身边所做的,效果很好。您可以与您的代码进行比较:

    在Startup.cs -> ConfigureServices方法中,添加这行代码services.AddDirectoryBrowser():

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
    
    
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDirectoryBrowser();
        }
    

    在 Startup.cs -> Configure 方法中,添加app.UseDirectoryBrowser(xxx):

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
    
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
                RequestPath = "/MyImages"
            });
    
            app.UseDirectoryBrowser(new DirectoryBrowserOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
                RequestPath = "/MyImages"
            });
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    

    发布到azure后,可以看到images目录下的所有文件,甚至多次刷新:

    【讨论】:

    • 这和我做的没有什么不同,我会尝试配置一下,看看是否可以解决,有趣的是文件下载正常,虽然目录浏览器没有列出.
    • @NullReference,是不是你的浏览器有问题,可以用其他浏览器测试吗?为了缩小这个问题的范围,你能不能只创建一个新的 web 项目,然后发布到 azure 看看它是否仍然存在。
    • 所以我尝试了多次更改但没有运气,我注意到它不是文件扩展名而是显示的文件数量,基本上浏览器将显示所有子目录和目录中的一个文件(如果文件夹中存在其他目录)或零文件,如果没有子目录。
    【解决方案2】:

    似乎只有 PhysicalFileProvider 接收到的 ExclusionFilters 参数等于 None,问题才会消失。这使问题消失了。以防万一其他人遇到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2013-05-03
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多