0

I'm working on .NET 4.7.2 When I define the path with a string, it doesn't work, but defines it in the tag it's working.

@{
    ViewBag.Title = "Home Page";
}

@{
    string path = Server.MapPath("~/Content/img/logo.jpg");
}

<img src="@path" alt="Alternate Text" />
<img src="~/Content/img/410.jpg" />
5
  • 1
    What does doesn't work mean? What HTML is this producing? Commented Nov 26, 2019 at 17:23
  • Doesn't show image Commented Nov 26, 2019 at 17:24
  • 1
    You can use like this too <img src='@Url.Content("~/Content/img/logo.jpg")' alt="Alternate Text" /> Commented Nov 26, 2019 at 17:28
  • 1
    Server.MapPath returns the path on the server, like c:\... it doesn't return the path on the website like http://... Try a view source to see Commented Nov 26, 2019 at 17:28
  • @DoğukanT.: Doesn't show image doesn't tell me what HTML is produced. Commented Nov 26, 2019 at 17:28

1 Answer 1

2

Server.MapPath returns a server based physical path. More than likely the web browser wont handle that path. You need to use a virtual path:

@{
    string path = Url.Content("~/Content/img/logo.jpg");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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