在我的代码中找不到显示图像的错误


嗨,我

有我以前的网络应用程序中的这段代码。我刚刚复制并粘贴到一个新站点,但图像未显示在新站点中。我什至键入代码来显示路径,它显示正常,显示id,它显示ok和产品中的所有属性,它们都显示正常,但图像不显示。当 F5 我的应用程序时,网站运行正常。但是,当无法加载IMG时,它只显示小图标,而不是图像。然而,当从浏览器使用检查器时,src atribute 具有正确的图像路径。我找不到问题所在这是代码

这是类别类

namespace Mysite.Models
{ 
    public class Category
    {
        //[ScaffoldColumn(false)]
        public int CategoryID { get; set; }
        [Display(Name = "Category Name")]
        public string CategoryName { get; set; }
        //[Display(Name = "Product Description")]
        public string Description { get; set; }
        public string ImagePath { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }
}

这是产品类别

namespace Mysite.Models
{
    public class Product
    {
        //[ScaffoldColumn(false)]
        public int ProductID { get; set; }
        [Required, StringLength(100), Display(Name = "Product Name")]
        public string ProductName { get; set; }
        [Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
        public string Description { get; set; }
        public string ImagePath { get; set; } // Here is the image path that I want to show           
        public int? CategoryID { get; set; }
        public virtual Category Category { get; set; }
    }
}

这是类别控制器,其方法浏览包括产品

namespace Mysite.Controllers
{
    public class CategoriesController : Controller
    {
               private ProductContext db = new ProductContext();
              public ActionResult Browse(string categories)
        {
            var categoriaModel = db.Categories.Include("Products")
                .SingleOrDefault(c => c.CategoryName == categories);
            return View(categoriaModel);/*(db.Categories.C)*/
        }

这是测试我是否要在浏览器中显示的所有内容的简化视图,实际上都会显示

@model Mysite.Models.Category
@{
    ViewBag.Title = "Browse";
}
<h2>Browse</h2>
@foreach(var item in Model.Products) { 
<p>@item.ProductName , @item.ProductID , @item.ImagePath</p>
<img  src="@item.ImagePath"/>
}

但这就是它实际显示的内容

Horizontal Faux Wood , 1 , Content/ProductImages/woodblind2.jpg /*small non-loaded picture icon here*/
Horizontal Real Wood , 2 , Content/ProductImages/woodblinds1.jpg /*small non-loaded picture icon here*/
Aluminium mini blinds , 10 , Content/ProductImages/aluminiumminiblind.jpg /*small non-loaded picture icon here*/

同样的代码,我在另一个站点中工作。我仔细检查了图像文件夹,路线,一切都很好

将其更改为:

<img src="@Url.Content("~/" + item.ImagePath)"/>

试试这个

<img src= "@Url.Content(item.ImagePath)" alt="Image" />

<img src="~/@item.ImagePath" />

本质上,~符号允许您引用与根路径相关的文件。

您在指定root ImagePath之前缺少第 / 个,我宁愿将string变量从另一个""中取出。无论如何,这就是我的方法

最新更新