gpt4 book ai didi

asp.net-mvc - 当 asp.net 核心应用程序在 docker 容器中运行时,UseStaticFiles 不起作用

转载 作者:行者123 更新时间:2023-12-04 17:48:47 25 4
gpt4 key购买 nike

我正在尝试将 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"]

最佳答案

我在 dockerfile 中犯了一些错误。

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

COPY app/* /app/

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

COPY app/ /app/

关于asp.net-mvc - 当 asp.net 核心应用程序在 docker 容器中运行时,UseStaticFiles 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com