【问题标题】:Getting HTTP Error 500.30 - PhysicalFileProvider获取 HTTP 错误 500.30 - PhysicalFileProvider
【发布时间】:2020-09-16 04:11:47
【问题描述】:

我想对一些只有授权用户才能访问的文件使用 wwwroot 之外的路径并找到 PhysicalFileProvider 方法。

我的初创公司看起来像:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();


        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), "Files")),
            RequestPath = "/Files"
        });
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

我认为这里没什么特别的,可以在我的本地机器上工作。 将此上传到托管服务器(主机:smarterasp.net)后,我收到 HTTP 错误 500.30 - ANCM 进程启动失败。如果我注释掉新的 StaticFileOptions 它可以工作,当然该文件夹是不可访问的。

使用 PhysicalFileProvider 时我还有什么事情要做吗?

使用 .Net Core 3.1

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-mvc-4


    【解决方案1】:

    我重现了你的错误,错误是找不到路径引起的。我通过将静态文件夹添加到发布文件夹中来解决。

    这是我的 startup.cs 的部分代码:

    app.UseStaticFiles();
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(
                        Path.Combine(Directory.GetCurrentDirectory(), "Files")),
                    RequestPath = "/Files"
                });
    

    这是我的部分观点代码:

    <img src="~/Files/banner1.svg" alt="ASP.NET" class="img-responsive" />
    

    发布文件夹包含发布所需的内容,我将文件文件夹添加到发布文件夹中。然后它就可以工作了。

    【讨论】:

    • 非常感谢您的回答。可以这么简单。我唯一拥有的是我没有发布文件夹。我在我的根目录中添加了 Files 文件夹,并且必须在项目文件中添加 。非常感谢!
    • “文件”文件夹仍然可以通过 url/files/{filename}.{extension} 直接访问,有什么想法可以解决这个问题吗?这是添加此文件夹的唯一原因...
    猜你喜欢
    • 2021-06-27
    • 2023-02-15
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2019-08-25
    • 2021-07-16
    • 2020-08-23
    • 2019-06-12
    相关资源
    最近更新 更多