简体   繁体   中英

How to get a web app to serve static files from a different server

I have an ASP.Net Core 2 MVC webapp that needs to retrieve and serve a pdf that's stored on a different server on the same LAN. It knows the full pathname. This code works well while developing on local machine (I stripped down the code to the bare minimum to get to the point):

public IActionResult GetPdf()
{
    FileStream fileStream = new FileStream(@"\\SRV1\Drawings\mydrawing.pdf", FileMode.Open, FileAccess.Read);
    var fsResult = new FileStreamResult(fileStream, "application/pdf");
    return fsResult;
}

However, when I publish the app on the server where I'm testing the deployment, I get this error:

System.IO.FileNotFoundException: Could not find file '/var/www/myapp/\\SRV1\Drawings\mydrawing.pdf'

Now, I get it that the app is only supposed to serve static files from within its wwwroot, so that doesn't surprise me. Therefore, based on this post and similar other posts, I added this code to Startup.cs:

app.UseFileServer(new FileServerOptions
{
    FileProvider = new PhysicalFileProvider(@"\\SRV1\Drawings"),
    RequestPath = new PathString("/PdfDrawings"),
    EnableDirectoryBrowsing = false
});

and changed the controller's action like so:

public IActionResult GetPdf()
{
    FileStream fileStream = new FileStream(@"\PdfDrawings\mydrawing.pdf", FileMode.Open, FileAccess.Read);
    var fsResult = new FileStreamResult(fileStream, "application/pdf");
    return fsResult;
}

but this way it doesn't work neither on the development machine nor on the server, as both fail to find the path. The only difference between the two is that the local machine will run the app, and return this error only when I request the GetPdf action, because it will point to C:\\

DirectoryNotFoundException: Could not find a part of the path 'C:\PdfDrawings\mydrawing.pdf'.

while the deployed app will not even run because a similar error occurs at the very start, while executing the Startup's Configure method.

I also tried this in Startup.cs:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(@"\\SRV1\Drawings"),
    RequestPath = "/PdfDrawings"
});

I was expecting to encounter credentials issues, but I never got that far. I'm also aware that there may be security concerns with this approach, but the app will reside in the LAN and will only be used by trusted personnel, so that's not an issue.

Additional info, in case it matters: I'm deploying the app to an Ubuntu server 18.04. The files I'm trying to access are on a different server in the same LAN.

Ultimately, the app will need to read and write on this second server also in other parts of code, so would someone be kind enough to point me to a post/tutorial that explains how to achieve that?

Your Ubuntu server does not understand Windows share paths. Simply try to access the path you provided on your Ubuntu server and you'll see the issue.

You will have to mount the share on your server to be able to access it. You also will have to use a different path for your local development and your deployment.

On Ubuntu it may be something like /mnt/srv1/Drawings (provided you mounted the share in /mnt/srv1) while on windows your path stays the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM