Asp.Net Core 2.0 - 检索图像URL

时间:2018-05-22 06:30:13

标签: c# asp.net-core .net-core

我正在尝试创建一个公开一些数据的restful服务,一切都很好,直到我意识到从服务器公开图像完整URL很痛苦。也许这只是我,但我发现它非常复杂。

只是在上下文中,我使用Entity Framework从我的数据库中读取,这是我正在使用的表:

enter image description here

正如你所看到的,有一个名为“ImagePath”的字段,坏名字,它应该是“ImageName”,无论如何,该列保存了代表该Champion对象的图像的确切文件名,所有图像目前位于wwwroot项目文件夹中,如下所示:

enter image description here

所有业务逻辑都不在控制器中,位于常规类的服务文件夹/层中(也许这不相关,但我只想说明我不在任何控制器内工作):

enter image description here

这就是我期待魔术发生的方法:

public async Task<IList<Champion>> GetChampions()
{
   List<Champion> champions = await _context.Champion
   .Include(x => x.PrimaryClass)
   .Include(x => x.SecondaryClass)
   .Include(x => x.ChampionUserRate)
   .ToListAsync();

   //BEFORE RETURNING THE CHAMPION TO THE CONTROLLER, I WILL NEED TO CONCATENATE MY CURRENT SERVER URL AND THE PATH TO THE IMAGE PLUS THE IMAGE NAME FILE WHICH IS ALREADY IN THE DATABASE AND ALREADY MATCHES THE IMAGES FOLDER

   string serverPathToChampionsFolder = null; //Example: http://localhost:57435/wwwroot/Champions

   foreach (var champion in champions)
   {
       champion.ImagePath = serverPathToChampionsFolder + champion.ImagePath;
   }

return champions;
}

在这里要清楚,这里的关键是:

 string serverPathToChampionsFolder = null; //Example: http://localhost:57435/wwwroot/Champions

我需要以某种方式获取当前URL,就像在示例中一样,将其添加到每个冠军中,以便客户端可以在图像标记内使用它。

如果通过我试图实现的方法无法做到这一点,我将接受任何其他建议,这里的重点是公开图像URL,无论如何。

2 个答案:

答案 0 :(得分:6)

基本上,您需要使用IHostingEnvironment并将其注入服务构造函数中。然后使用wwwroot中的文件夹名称创建一个字符串变量,例如"Champions"

以下是示例代码:

private readonly IHostingEnvironment hostingEnv;

private const string ChampionsImageFolder = "Champions";

public ChampionsService(IHostingEnvironment hostingEnv){
    this.hostingEnv = hostingEnv;
}

// Suppose this method is responsible for fetching image path
public string GetImage(){
    var path = Path.Combine(hostingEnv.WebRootPath, ChampionsImageFolder);

    return path;
}

IHostingEnvironment接口的作用是“提供有关运行应用程序的托管环境的信息。”

如果你想获取给定路径中的文件,这会给你一个提示。

var directoryFiles = Directory.GetFiles("wwwroot/Champions");

foreach (var item in directoryFiles)
{
    // do something here
}

如果您想从这些wwwroot文件夹创建路径链接,则需要在启动时注册UseDirectoryBrowser

在Startup.cs文件中,找到Configure方法插入此代码段

这些代码段将公开Champions目录中的文件,并在您的网站上创建一个新的路线ChampionImages,该路线来自您Champions wwwroot >

app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "Champions")),
    RequestPath = new PathString("/ChampionImages")
});

然后,您现在可以使用类似localhost:8080/ChampionImages的内容,您可以在其中查看存储在wwwroot的Champions文件夹中的每个文件。你可以做些什么来创建该图像的URL。

var imageUrl = $"/ChampionImages/{exactFileName}"; // this will create a string link.

我希望这个简单的代码片段能为您提供帮助或想法:)

答案 1 :(得分:0)

// Enable directory browsing
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(
           Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")),
                RequestPath = new PathString("/images")
            });
         
            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")),
                RequestPath = new PathString("/images")
            });

上面的答案是正确的,但仍然无法加载图片url,所以需要在startup.cs文件中添加几行,我在这里发布