Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

asp.net mvc - When running ASPNET Core application in Ubuntu 16.04 how can I access a file in another folder?

I am trying to run an ASP5/MVC6 ASPNET core application in Ubuntu 16.04 using dotnet core 1.1. My application resides in one directory off of the users home directory, and another directory off the home directory holds a folder structure with image (.jpg) files.

I am receiving the following error...

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: Access to the path '/home/myuser/myimages/somefolder' is denied.
System.UnauthorizedAccessException: Access to the path '/home/myuser/myimages/somefolder' is denied. ---> System.IO.IOException: Permission denied

Here is an example of the folder structure...

/home/myuser/mywebapp/mywebapp.dll

/home/myuser/myimages/somefolder/someimage.jpg

I Have chmodded the following directories to 777..

  • /home
  • /home/myuser
  • /home/myuser/myimages
  • /home/myuser/myimages/somefolder

... and I have chmodded file someimage.jpg to 777 as well.

I am running the aspnet core application from /home/myuser/mywebapp. I have tried sudo and no good.

Can I reference files in ASPNET Core web app that are in a directory outside of the working directory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default static files should be located in the wwwroot folder, which inside you working folder. But if you want to keep them outside you can configure the static files middleware:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseStaticFiles();

    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
        RequestPath = new PathString("/StaticFiles")
    });
}

See tutorial here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...