4

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.

5
  • 1
    why you are adding images in Views directory? you should'nt Commented Sep 18, 2016 at 10:22
  • @EhsanSajjad, but why mvc could not found the file? Commented Sep 18, 2016 at 10:29
  • try this src="@Url.Content("~/Views/Home/download.png")" Commented Sep 18, 2016 at 10:53
  • @M.Azad, did not worked Commented Sep 18, 2016 at 11:01
  • Because views folder is not for that purpose Commented Sep 18, 2016 at 12:10

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Its because of the inbuilt security of ASP MVC to protect your views being accessed directly. Check removing 'BlockViewHandler' handler entry from ~/Views/web.config

My advice is to move image to 'Content' folder and keep 'Views' folder clean.

Comments

3

Any MVC Version not allow to load other resource except html from Views folder. 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.

for better information please check file header request and response in browser (development tool/F12) network tab.

Response of image from Views:

enter image description here

Response of image from content folder:

enter image description here

Comments

2

In .net core 3 you will also need this below line in startup.cs

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

1 Comment

It works, acctually, you should just change @"wwwroot\images" by @"wwwroot/images"
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.