asp.net核心RC2图像不显示 [英] asp.net core RC2 image does not display

查看:60
本文介绍了asp.net核心RC2图像不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是显示Web根文件夹中的图像,这就是我试图做的方式:

What I am trying to do is to display an image from web root folder and this is the way I am trying to do it:

下面的课程只是实验性的,只是我尝试的一个示例.目前,正在读取的文件夹中只有一张图像. 同样,rootPath也来自:_hostingEnvironment.WebRootPath

The following class is just experimental and is just and example for me to try it out. For now there is only one image in the folder that is being read from. As well the rootPath is taken from: _hostingEnvironment.WebRootPath

 public class GetRandomImageForGalleryView : IGetRandomImageFromFolder
    {
        private string rootPath;
        public GetRandomImageForGalleryView(string rootPath)
        {
            this.rootPath = rootPath;
        }
        public string[] getImage()
        {
            return ReadPhotosFromDirectory();
        }
        private string[] ReadPhotosFromDirectory()
        {

            string[] fileLocations = Directory.GetFiles(rootPath+"\\lib\\Images\\Nature");
            return fileLocations;
        }
    }

这就是我试图显示它的方式:

And this is the way I am trying to display it:

@model IGetRandomImageFromFolder
@{ 
    ViewBag.Title = "Gallery";
}
<div class="container">
    <div class="row">
        @{
            foreach (string item in Model.getImage())
            {
                <img src="@item" alt="Image" />
            }
        }
    </div>
</div>

但是,即使我将其更改为<img src="@Url.Content(item)" alt="Image" />,也没有任何输出.

However there is no output, even if I change it to <img src="@Url.Content(item)" alt="Image" /> Still nothing is happening.

如果我只是输出@item,它将显示图像的路径. app.UseStaticFiles();也已添加.

If I just output @item it will display the path to the image. The app.UseStaticFiles(); has been added as well.

所以我的问题是我在做什么错,我错过了什么吗?以及如何正确执行呢?

So my question is what am I doing wrong, am I missing something? And how to do it properly?

推荐答案

为wwwroot文件夹中的图像创建一个文件夹 您可以使用此图片

Create a folder for images in wwwroot folder and you can access image with this

<img src="~/images/123.jpg" />

或使用url.content

or using url.content

  <img src='@Url.Content("~/images/123.jpg" )'/>

这篇关于asp.net核心RC2图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆