Reputation: 2830
In my small blazor project I want to display an image in a view as follows:
<img scr="img/background.jpg" class="img" alt="background">
But all I see in the browser is the 'alt'-text (sorry for the dark image, it's a dark themed website):
The image is located in wwwroot/img/background.jpg. Curiously enough in the same location is the icon of the webpage, which I call within the head
portion of _Host.cshtml
as follows
<link rel="icon" type="image/x-icon" href="/image/icon.png">
and that is showing fine.
Following other similar posts like this one, I have already tried to change the source path to any of the following:
scr="/img/background.jpg"
scr="~/img/background.jpg"
scr="img/background.jpg"
scr="~img/background.jpg"
None of which seem to work. So there are two questions: 1. What could be the issue to my problem and 2. How do I debug such "wrong path" type of errors. For the second part, my intuition would be to use the "view source" option in the browser and look for the image, but so far I have not found the location of the image as the server presents it to the browser. I have also tried to use a different browser, but with the same result.
Edit:
Navigating to the image with https://localhost:7001/img/background.jpg
directly works as well. It just doesn't want to show from within the html for some reason...
Upvotes: 4
Views: 1056
Reputation: 21304
I have found the intellisense in Visual Studio added a path during development that seemed valid, but would not work at runtime. I had to change the following:
scr="~/myImage.jpg"
to the following:
src="myImage.jpg"
The location of the image in my example was in the root of wwwroot
, so wwwroot/myImage.jpg
Upvotes: 1
Reputation: 273179
<img scr="img/background.jpg" class="img" alt="wrong">
<img src="img/background.jpg" class="img" alt="ok">
See what i did there?
Upvotes: 5
Reputation: 273179
- How do I debug such "wrong path" type of errors.
That is a good question.
With your App running, hit F12 in your Browser. Make sure you can see the "Dev Tools".
Select the Network tab and reload your page. Find the 404 error.
Upvotes: 2