Search code examples
c#asp.netasp.net-mvcrazorasp.net-mvc-5

Asp.Net Mvc 5 image not displaying


I have same images in Content and Views folders. 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:

Failed to load resource: the server responded with a status of 404 (Not Found)

enter image description here

But actually, the resource is there. Here are my folders:

enter image description here

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.


Solution

  • Unless you changed the default configuration, folder Views contains file Web.config that has settings for restricting access to files in this folder:

    <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, e.g. Content.