Asp.Net Core 3.1 - 从目录中获取图像并在 razor 视图中显示?

Asp.Net Core 3.1 - Get Image from directory and display in razor view?

如何从目录中取出图片并显示出来?

“我看到了类似的帖子,但没有帮助”

我这样做了,但没用

控制器:

     string[] filePaths = Directory.GetFiles(Path.Combine(_hostingEnv.WebRootPath, @"site\img\banner\"));          
       
        ViewBag.ListImgMainDefalt = filePaths;

查看:

foreach (var item in ViewBag.ListImgMainDefalt)
                    {
                        <tr>                               

                            <td>
                                <img src="@item" />
                            </td>
                        </tr>

                    }

结果:

像下面这样更改您的代码:

string[] filePaths = Directory.GetFiles(Path.Combine(_hostingEnv.WebRootPath, @"site\img\banner\"));
List<string> fileName = new List<string>();
foreach (string filePath in filePaths)
{
    fileName.Add(@"/site/img/banner/"+Path.GetFileName(filePath));
}
ViewBag.ListImgMainDefalt = fileName;