简体   繁体   English

Asp.Net Mvc 5 图像不显示

[英]Asp.Net Mvc 5 image not displaying

I have same images in Content and Views folders.我在ContentViews文件夹中有相同的图像。 I am trying to display images as below:我正在尝试显示图像如下:

<img src="~/Content/Images/download.png" alt="Content folder" />
<br />
<br />
<img src="~/Views/Home/download.png" alt="Views folder" />
<br />

The image in Content folder displays successfully but the one in Views folder doesn't display and gives below error: Content文件夹中的图像显示成功,但 Views 文件夹中的图像不显示并给出以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found)加载资源失败:服务器响应状态为 404(未找到)

在此处输入图片说明

But actually, the resource is there.但实际上,资源就在那里。 Here are my folders:这是我的文件夹:

在此处输入图片说明

Could you please tell me which point I am doing wrong?你能告诉我我做错了哪一点吗? I am using MVC 5 with razor engine and Visual Studio 2015 community edition.我正在使用带有剃刀引擎和 Visual Studio 2015 社区版的 MVC 5。

Unless you changed the default configuration, folder Views contains file Web.config that has settings for restricting access to files in this folder:除非您更改了默认配置,否则文件夹Views包含文件Web.config ,该文件具有用于限制访问此文件夹中文件的设置:

<system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" 
           type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

So you can either remove this handler (which is not recommended for security purposes), or move your static files to some other folder, eg Content .因此,您可以删除此处理程序(出于安全目的不建议这样做),或者将您的静态文件移动到某个其他文件夹,例如Content

Its because of the inbuilt security of ASP MVC to protect your views being accessed directly.这是因为 ASP MVC 的内置安全性可以保护您的视图被直接访问。 Check removing ' BlockViewHandler ' handler entry from ~/Views/web.config检查从~/Views/web.config删除“ BlockViewHandler ”处理程序条目

My advice is to move image to ' Content ' folder and keep ' Views ' folder clean.我的建议是将图像移动到“ Content ”文件夹并保持“ Views ”文件夹干净。

Any MVC Version not allow to load other resource except html from Views folder.任何 MVC 版本都不允许从 Views 文件夹加载除 html 之外的其他资源 Because when you call a URL it waiting for text/html content type and because of that image inside main Views folder not able to render in page.因为当您调用 URL 时,它会等待text/html 内容类型,并且由于主 Views 文件夹中的图像无法在页面中呈现。

for better information please check file header request and response in browser (development tool/F12) network tab.有关更好的信息,请检查浏览器(开发工具/F12)网络选项卡中的文件头请求和响应

Response of image from Views:来自视图的图像响应:

在此处输入图片说明

Response of image from content folder:来自内容文件夹的图像响应:

在此处输入图片说明

In .net core 3 you will also need this below line in startup.cs在 .net core 3 中,您还需要在 startup.cs 中的以下行

app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")),
                RequestPath = "/wwwroot/images"
            });

\n
\n
\n
<img src="@Url.Content("~/Views/Home/download.png")" alt="Views folder" />
\n
\n
\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM