【问题标题】:MVC Core 1.1 - Image will not show on View, cannot find path or something.MVC Core 1.1 - 图像不会显示在视图上,找不到路径或其他东西。
【发布时间】:2018-05-02 06:10:32
【问题描述】:

长期 .NET 开发人员,但在 MVC 方面是新手。使用 MVC Core 1.1。

我添加了一个内容文件夹和一个图像文件。我也使用了 URL.Content 和直接标签。图像始终显示为“盒装 X”符号。这让我非常自卑。

查看:

@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3>

<img src="~/Content/santa.jpg" alt="Sample Image"  />
<img src="@Url.Content("~/Content/santa.jpg")" />
<p>Use this area to provide additional information.</p>

解决方案资源管理器图片在这里: enter image description here

感谢您在这里提供的任何帮助。我不明白。使用 VS 2017 企业版。

【问题讨论】:

  • 不确定,但请检查解决方案资源管理器中的图像是否设置为属性下的内容。

标签: c# asp.net .net image model-view-controller


【解决方案1】:

在 ASP.NET 核心中,静态文件默认仅来自 wwwroot 文件夹。这意味着如果您尝试从 ~/Contents(您的应用根目录中的一个目录)访问文件,它将无法正常工作。

好消息是,您可以根据需要配置静态文件位置。因此,请在 Startup 类中更新您的 Configure 方法。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
                                                           ILoggerFactory loggerFactory)
{
     // Your existing code goes here

     app.UseStaticFiles();

     app.UseStaticFiles(new StaticFileOptions()
     {
        FileProvider = new PhysicalFileProvider(
              Path.Combine(Directory.GetCurrentDirectory(), @"Content")),
        RequestPath = new PathString("/Images")
     });
}

现在可以像yourSiteName/Images/santa.jpg这样从浏览器访问这些图片

<img src="~/Images/santa.jpg" alt="Sample Image"  />

您也可以考虑将图像移动到wwwroot 目录,因为 ASP.NET 团队为此目的添加了该目录(静态资产)。我强烈推荐这个。

如果您将图像文件移动到wwwroot 目录内名为images 的文件夹中,您可以简单地使用~/images/santa.jpg,并且无需我们进行上述自定义配置即可工作。

<img src="~/images/santa.jpg" alt="Sample Image"  />

【讨论】:

  • 我非常爱你。哈哈。谢谢你。这是我在驯​​服这种被称为 MVC 的野兽时完全逃脱的另一件事。
猜你喜欢
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 2013-02-05
  • 2021-08-30
相关资源
最近更新 更多