簡體   English   中英

asp.net核心RC2圖像不顯示

[英]asp.net core RC2 image does not display

我想做的是顯示Web根文件夾中的圖像,這就是我試圖做的方式:

下面的課程只是實驗性的,只是我嘗試的示例。 目前,正在讀取的文件夾中只有一張圖像。 同樣, _hostingEnvironment.WebRootPath也來自: _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;
        }
    }

這就是我試圖顯示它的方式:

@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" />也沒有任何輸出。

如果我只輸出@item ,它將顯示圖像的路徑。 app.UseStaticFiles(); 也已添加。

所以我的問題是我在做什么錯,我錯過了什么嗎? 以及如何正確地做呢?

在wwwroot文件夾中為圖像創建一個文件夾,您可以使用此文件夾訪問圖像

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

或使用url.content

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

它只允許wwwroot文件夾下的文件。 因此,在路徑下方僅需提及。 隨附示例快照。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM