1

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:

enter image description here

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):

enter image description here

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"))
        });
Paulo
  • 577
  • 3
  • 8
  • 23

3 Answers3

9

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?

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"
});
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • apparently the working directory is correct. It is one level above the "wwwroot" folder. I edited the post, look the image. – Paulo Jan 14 '19 at 11:03
  • This solution works. wwwroot is finnicky for me. In my case, it's best for outside generated content to be served from a different root directory then wwwroot – Paul Carlton Jan 26 '23 at 20:34
5

If you are using visual code then you have to set up your working directory in cwd parameter of configuration array of your lunch.json file. See attached screenshot.

enter image description here

4

use IHostingEnvironment

app.UseStaticFiles(new StaticFileOptions
{
     FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Assets")),
            RequestPath = "/Assets"
});
Seven
  • 134
  • 3