Search code examples
asp.net-corevirtual-directory

Upload Folders to Fileshare with ASP.NET Core Website


I want my Uploads folder to reside on a fileshare.

Reason why I want this: Redundant frontend.

So instead of saving to:

C:\inetpub\wwwroot\wwwroot\Uploads

Uploads should be saved to:

\\fileshare01\MyWebsite\Uploads

I am aware that VirtualDirectories exist. This works for reading from the fileshare but writing to the Uploads directory still writes to lokal drive. So with VirtualDirectories I can access http://localhost/Uploads/myfile.png which is actually on the fileshare BUT new files are not written there!

Here (simplified) how I save files:

IFormFileCollection files = Request.Form.Files;
var file = files.First();
using (var stream = new FileStream(@"Uploads\myfile.png", FileMode.Create))
{
    await file.CopyToAsync(stream);
}

When I try to save to the new network path as absolute path it seems I require higher permissions and end up with a 500.30 error. I guess because application pool user has too little permission which I think is a good thing.

My Question:

How is this problem solved as good-practice? Shouldn't everything work automagically when configuring a VirtualDirectory including writing?


Solution

  • Solved it. I just got the 500.30 error because of an error in my appconfig.json. I didn't escape the backslashes in my base path.

    I found this blog post saying

    There is no need to add a „virtual directory“ in IIS, this stuff is deprecated

    and explaining that this is the way it's done via Startup.cs Configure() method:

    app.UseFileServer(new FileServerOptions
    {
        FileProvider = new PhysicalFileProvider(@"\\server\path"),
        RequestPath = new PathString("/MyPath"),
        EnableDirectoryBrowsing = false
    });
    

    Another configuration mystery of ASP.NET Core solved :)