asp.net 核心不加载静态文件
asp.net core not loading static files
我有一个应用程序 .NET core 2.1,代码如下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Assets")),
RequestPath = "/Assets"
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
和我的结构文件夹:
但是 none 这些 url 打开图像:
"my-website.com/images/snes/alien.jpg"
"my-website.com/wwwroot/images/snes/alien.jpg"
"my-website.com/Assets/Snes/alien.jpg"
有人知道哪里出了问题吗?
编辑:这是通过 CurrentDirectoy() 方法获取的文件夹(显然是正确的):
Edit2:使用此代码可在本地主机上运行,但当我在 azure 上发布时则不行:
app.UseFileServer(
new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
});
您所处的工作目录很可能与您想象的不同。请通过在 foo:
上设置断点来检查这一点
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var foo = Directory.GetCurrentDirectory();
}
解决方案取决于您如何启动应用程序。
如果您通过 Visual Studio 执行此操作,可能您在项目属性中设置了另一个工作目录?
如果通过命令行,您需要cd
到您的项目根目录。
另一种解决方案是使用 directory of your assembly:
// get the directory
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
var assetDirectory = Path.Combine(assemblyDirectory, "Assets"));
// use it
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(assetDirectory),
RequestPath = "/Assets"
});
使用IHostingEnvironment
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Assets")),
RequestPath = "/Assets"
});
如果您使用可视化代码,则必须在 lunch.json
文件的 configuration
数组的 cwd
参数中设置您的工作目录。请参阅随附的屏幕截图。
我有一个应用程序 .NET core 2.1,代码如下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Assets")),
RequestPath = "/Assets"
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
和我的结构文件夹:
但是 none 这些 url 打开图像:
"my-website.com/images/snes/alien.jpg"
"my-website.com/wwwroot/images/snes/alien.jpg"
"my-website.com/Assets/Snes/alien.jpg"
有人知道哪里出了问题吗?
编辑:这是通过 CurrentDirectoy() 方法获取的文件夹(显然是正确的):
Edit2:使用此代码可在本地主机上运行,但当我在 azure 上发布时则不行:
app.UseFileServer(
new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
});
您所处的工作目录很可能与您想象的不同。请通过在 foo:
上设置断点来检查这一点public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var foo = Directory.GetCurrentDirectory();
}
解决方案取决于您如何启动应用程序。
如果您通过 Visual Studio 执行此操作,可能您在项目属性中设置了另一个工作目录?
如果通过命令行,您需要cd
到您的项目根目录。
另一种解决方案是使用 directory of your assembly:
// get the directory
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
var assetDirectory = Path.Combine(assemblyDirectory, "Assets"));
// use it
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(assetDirectory),
RequestPath = "/Assets"
});
使用IHostingEnvironment
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Assets")),
RequestPath = "/Assets"
});
如果您使用可视化代码,则必须在 lunch.json
文件的 configuration
数组的 cwd
参数中设置您的工作目录。请参阅随附的屏幕截图。