img src is not working for static image in mvc view

7.9k views Asked by At

In view I want to show image which is store in view folder named Errorpage.png. Now, I want to show that image in Error.cshtml view file.

Here is my code

<img src="~/Views/Errorpage.png" alt="error">

Now , this code is not working and showing error alt in the place of image. While the path is perfectly right, but surprised that why it is not working.

While I tried by fetch the online image and that is work.

Here is the code

<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/anton-ploom.jpg" 
    alt="A danseur in brisé (a leap with the legs together) on a blue background">

Now, I am confused that this second code is working properly while the first code is not working.

The syntax is same both of the code.

Thank You.

3

There are 3 answers

4
Vishvadeep singh On BEST ANSWER

Try with this way:

can you please put your image in image folder outside Views folder that can work

or

  <img alt="error" src="@Url.Content("/Views/Errorpage.png")" />

if that doesn't work you could try with this way:

<img alt="error" src="/Views/Errorpage.png" /> 

or there is another solution if all are fails :

<img alt="error" src="<%= Url.Content("~/Views/Errorpage.png") %>" />
1
amir4545 On

in visual studio, the image works from ~/contents/*.jpeg directory. if you will put it in any other directory it will not work.

0
Syed Akhter On

try this

 <img  src="@Url.Content("~/Views/Errorpage.png")"  alt="error"/>