Takumasakix
Takumasakix

Reputation: 33

Cannot render an image using Razor View

Problem: Even though the image path is correct, the image cannot be rendered on a browser using Razor View. Also, the image is not corrupted because a HTML file can reference to the image properly.

Code:

Index.cshtml

@{
    ViewData["Title"] = "Home Page";
    var workingDirectory = System.Environment.CurrentDirectory;
    var projectDirectory = System.IO.Directory.GetParent(workingDirectory).Parent.Parent.FullName;
    var path = System.IO.Path.Combine(workingDirectory, "wwwroot", "images", "sampleImage.png");
}

<div class="text-center">
    <p>@System.IO.File.Exists(path)</p> // returns True

    <img src="@path" alt="error" />
</div>

That would be really nice if anyone could help this problem!!

Upvotes: 1

Views: 218

Answers (1)

bwakabats
bwakabats

Reputation: 703

File.Exists is run on the server. src is requested from the client. Try

src = "@Url.Content("~/images/sampleImage.png")"

Upvotes: 1

Related Questions