5

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"))
        });
3
  • 2
    what is your current working directory? Commented Jan 14, 2019 at 1:40
  • 2
    Can you try this? stackoverflow.com/a/47385434/495455 Commented Jan 14, 2019 at 4:36
  • 1
    How did you publish to Azure? Fail to reproduce your issue, this "my-website.com/Assets/Snes/alien.jpg" should be able to open this image. Did you get 404 error or anything else? Commented Jan 15, 2019 at 2:57

3 Answers 3

13

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"
});
Sign up to request clarification or add additional context in comments.

2 Comments

apparently the working directory is correct. It is one level above the "wwwroot" folder. I edited the post, look the image.
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
8

use IHostingEnvironment

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

2 Comments

I am using ASP.NET Core 3.1 and this worked best for me.
worked net core 8
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.