简体   繁体   English

如何让 Web 应用程序从不同的服务器提供静态文件

[英]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.我有一个 ASP.Net Core 2 MVC webapp,它需要检索和提供存储在同一 LAN 上的不同服务器上的 pdf。 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.现在,我知道该应用程序只应该从其 wwwroot 中提供静态文件,所以这并不让我感到惊讶。 Therefore, based on this post and similar other posts, I added this code to Startup.cs:因此,基于这篇文章和其他类似的文章,我将这段代码添加到 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:\\两者唯一的区别是本地机器会运行app,只有在我请求GetPdf动作的时候才返回这个错误,因为它会指向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.而部署的应用程序甚至不会运行,因为在执行 Startup 的 Configure 方法时,一开始就发生了类似的错误。

I also tried this in Startup.cs:我也在 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.我也知道这种方法可能存在安全问题,但该应用程序将驻留在 LAN 中,并且只能由受信任的人员使用,因此这不是问题。

Additional info, in case it matters: I'm deploying the app to an Ubuntu server 18.04.附加信息,以防万一:我正在将应用程序部署到 Ubuntu 服务器 18.04。 The files I'm trying to access are on a different server in the same LAN.我尝试访问的文件位于同一 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.您的 Ubuntu 服务器不了解 Windows 共享路径。 Simply try to access the path you provided on your Ubuntu server and you'll see the issue.只需尝试访问您在 Ubuntu 服务器上提供的路径,您就会看到问题。

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.在 Ubuntu 上,它可能类似于 /mnt/srv1/Drawings(前提是您将共享安装在 /mnt/srv1 中),而在 Windows 上,您的路径保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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