I have an app .NET core 2.1 with this code:
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();
}
and my structure folder:
but none of these urls open the image:
"my-website.com/images/snes/alien.jpg"
"my-website.com/wwwroot/images/snes/alien.jpg"
"my-website.com/Assets/Snes/alien.jpg"
anybody know what is wrong?
Edit: Here is the folder get by CurrentDirectoy() method (apparently is correct):
Edit2: With this code work on localhost but not when i publish on azure:
app.UseFileServer(
new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
});
You are most likely in a working directory that is different than the one you think. Please check this by setting a breakpoint on foo:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var foo = Directory.GetCurrentDirectory();
}
The solution depends in how you start the application.
If you are doing it via Visual Studio, probably you have set another Working Directory in the Project properties?
If via command line, you need to cd
to your project root.
Another solution, would be to use the 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"
});