简体   繁体   English

如何从.net Core中的另一台服务器访问static文件?

[英]how to access static files from Another server in .net Core?

I have 2 Web Apps in.Net Core, the first is called ' MiddelwareApp ' that is public To the inte.net and Receive Requests from clients in encrypted Case Then Decrypt them and sends Requests to the second App called " MainApp " located at another server.我在 .Net Core 中有2 个 Web 应用程序,第一个称为“ MiddelwareApp ”,它对 inte.net 是公共的,并在加密的情况下接收来自客户端的请求然后解密它们并将请求发送到位于另一个名为“ MainApp ”的第二个应用程序服务器。

'MainApp' contains My Controllers In addition to Static Files in WWWRoot. “MainApp”包含我的控制器以及 WWWRoot 中的 Static 个文件。 Sending Requests from MiddlewareApp, mapping to controllers in MainApp, and getting the correct data are done without any problem.从 MiddlewareApp 发送请求,映射到 MainApp 中的控制器,并获取正确的数据都没有任何问题。 But the problem with Static Files What I want is to access my static files in MainApp from Middlewareapp in Another server.但是 Static 文件的问题我想要的是从另一台服务器的 Middlewareapp 访问 MainApp 中的 static 文件。

Currently, I am in the local host MainApp in port 7000 and middleware in port 7001目前,我在7000 端口的本地主机 MainApp7001 端口的中间件

In program File of mainApp:在mainApp的程序文件中:

.........
app.UseHttpsRedirection();

//to allow access files in wwwroot without any server-side processing. 
app.UseStaticFiles(new PathString("/app-Assets"));
// to allow access files not in wwwroot
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), @"Assets")),
    RequestPath = new PathString("/app-Assets")
});
app.UseRouting();
......

I tried In the program file of Middleware app:我试过在中间件应用程序的程序文件中:

...
app.UseFileServer(

    new FileServerOptions
    {
        
         FileProvider = new PhysicalFileProvider(@"\\https:\\localhost:7000\app-Assets"),

// this is url of  static file in Main app
RequestPath = new PathString("/app-Assets"),
EnableDirectoryBrowsing = false
});
...

in my HTML I want to access Image directly from URL of middleware port 7001在我的 HTML 中,我想直接从中间件端口 7001 的 URL 访问图像

<img src="https://localhost:7001/app-Assets/myImage.jpg"\>

I expected that image src would send request To middleware then 'UseFileServer' in its program access static file of Main app on another server我预计图像 src 会向中间件发送请求,然后在其程序访问另一台服务器上的主应用程序的 static 文件中发送“UseFileServer”

The error say错误说

System.IO.DirectoryNotFoundException: '//https://localhost:7000/app-Assets/' System.IO.DirectoryNotFoundException: '//https://localhost:7000/app-Assets/'

but if i make path '{LocalDrive}/app-Assets/' it work但是如果我创建路径 '{LocalDrive}/app-Assets/' 就可以了

I Follow some guidelines for that but now work for me because我遵循一些指导方针,但现在为我工作,因为

https://www.jauernig-it.de/asp.net-coreiis-serving-content-from-a-file-share/ https://www.jauernig-it.de/asp.net-coreiis-serving-content-from-a-file-share/

How to use static content in .NET Core from server location? 如何从服务器位置使用 .NET Core 中的 static 内容?

if some one need some additional details,I will do如果有人需要一些额外的细节,我会做

Thanks for your helping谢谢你的帮助

Edit: The solution Worked For me编辑:解决方案对我有用

Using app.UseFileServer() method in Middleware App not Work at All,So I tried another way that make the same effect在中间件应用程序中使用app.UseFileServer()方法根本不起作用,所以我尝试了另一种产生相同效果的方法

[Route("GetImage")]
[HttpGet]
public async Task<IActionResult> Get(string filePath, bool inline = true)
{
    if (string.IsNullOrWhiteSpace(filePath))
        throw new ArgumentNullException(nameof(filePath));

    filePath = Path.Combine("app-Assets\\", filePath);

    var httpClient = _httpClientFactory.CreateClient("AnotherServerDirectory");
    var response = await httpClient.GetAsync(filePath);
    var ms = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
    var ext = Path.GetExtension(filePath).ToLowerInvariant();

    var cd = new ContentDisposition
    {
        Inline = inline // false = prompt the user for downloading;  true = browser to try to show the file inline
    };

    Response.Headers.Add("Content-Disposition", cd.ToString());
    return new FileStreamResult(ms, GetMimeTypes()[ext]);
} 

I set 'Content-Disposition' to control if I want to download the image or display it directly from <img src=""> tag then in Html I make我设置'Content-Disposition'来控制我是否想下载图像或直接从<img src="">标签显示它,然后在 Html 我做

<img src="https://localhost:700/GetImage?filePath=myImage.jpg"\>

If you only want the result, then you can use server to return to the client.如果你只想要结果,那么你可以使用服务器返回给客户端。

Example:例子:

  1.  <img src="https://localhost:7001/app-Assets?image=myImage.jpg"\>
  2. Stream image from controller's Stream 来自控制器的图像

    [HttpGet] [Route("app-Assets")] public IActionResult AssetsImage(string image) { var image = System.IO.File.OpenRead($"app-Assets/{image}"); return File(image, "image/jpeg"); }

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

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