dotnet / core

.NET news, announcements, release notes, and more!
https://dot.net
MIT License
20.85k stars 4.88k forks source link

Using virtual directory with ASP.NET Core 3.1 #4893

Open ekhtiari opened 4 years ago

ekhtiari commented 4 years ago

In ASP.NET framework 4.8 and below version the web application use IIS for processing and show data. Ans I web need to work with other resource like folders and network resource we can easily create Virtual Directory in IIS and map to physical path and in Asp.net application work like normal folder but in Net Core we use Kestrel and IIS work as a proxy and Virtual Directory not recognized by Kestrel. I use this code but not work in function. Write this section in Configure Function

app.UseFileServer(new FileServerOptions
{
 FileProvider = new PhysicalFileProvider(@"E:\\MyFiles"),
 RequestPath = new PathString("/PFiles"),
 EnableDirectoryBrowsing = true
});

var folder = Path.Combine(environment.WebRootPath, "temp");

                var fileName = Path.Combine(environment.WebRootPath, "temp\\test.jpeg");

                var basePath = Path.Combine(environment.WebRootPath, "PFiles");

                var yearPath = Path.Combine(basePath, "2020");
                var monthpath = Path.Combine(basePath, "2020", "06");

                if (!Directory.Exists(yearPath))
                {
                    Directory.CreateDirectory(yearPath);
                }
                if (!Directory.Exists(monthpath))
                {
                    Directory.CreateDirectory(monthpath);
                }

                var dpath = Path.Combine(basePath, "2020", "06");

                var destinationPath = Path.Combine(environment.WebRootPath, dpath);

                System.IO.File.Move(fileName, destinationPath);

but I receive Access denied Error or create directory inside of wwwroot folder. So, what must I do?

Pilchie commented 4 years ago

@shirhatti @jkotalik - any ideas here?

jkotalik commented 4 years ago

What is the app pool identity here? Is it running as LocalSystem or the AppPoolIdentity?

jkotalik commented 4 years ago

You may need it to be LocalSystem to write files to that location.

ekhtiari commented 4 years ago

You may need it to be LocalSystem to write files to that location.

if I change app pool identity I can access the virtual directory ? I think there no connection between Kestrel and IIS and Kestrel have no info about virtual directory that create in IIS