【问题标题】:UseStaticFiles not work when the asp.net core application is running in the docker container当 asp.net 核心应用程序在 docker 容器中运行时,UseStaticFiles 不起作用
【发布时间】:2017-10-16 10:10:03
【问题描述】:

我正在尝试将一个 asp.net 核心应用程序部署到 docker 容器中。该应用程序是使用 dotnet new mvc 创建的。当应用程序运行是非 docker 环境时,一切正常。但是,在 docker 容器中,浏览器无法加载 wwwroot 文件夹中的所有静态文件。 这是Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(),"wwwroot"))
            .UseUrls("http://*:5050")
            .Build();
}

这里是 Startup.cs

   public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

这里是 dockerfile

FROM microsoft/aspnetcore
RUN mkdir -p /app
COPY app/* /app/
WORKDIR /app
CMD ["dotnet","/app/app.dll"]

【问题讨论】:

  • 什么返回Directory.GetCurrentDirectory()?可能它映射到错误的目录

标签: asp.net-mvc asp.net-core


【解决方案1】:

我在 dockerfile 中犯了一些错误。

以下命令无法递归复制源目录

COPY app/* /app/

相反,COPY 命令应更正如下以支持递归

COPY app/ /app/

【讨论】:

  • 谢谢!我犯了完全相同的错误.. 花了很长时间才解决这个问题!
猜你喜欢
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 2019-09-03
  • 2023-03-27
  • 2018-12-13
  • 2021-01-30
  • 2019-01-26
  • 2021-11-12
相关资源
最近更新 更多