Donvan
Donvan

Reputation: 61

MVC5 and Javascript relative image paths confusion

My mvc5 webApp can´t seem to display images if I run it on the virtual server, however if I run it locally it works.

So I have tried several approaches and none that allowed me to run the app on the virtual server or on my pc worked I have tried:

(these worked if I was running this locally)

Images/arrow.png

/Images/arrow.png

This path works for the virtual server but not locally.

webAPP/Images/arrow.png

I know about @Url.Action but I have a lot of different images, around 15, and I don´t know if using @Url.Action is a good idea for that many.

any small example would help tremendously!

Upvotes: 2

Views: 5034

Answers (2)

scgough
scgough

Reputation: 5252

If you know the path to the images from the site root you can do the following in MVC:

@Url.Content("~/images/my-image.jpg")

The "~/" will map from the site root.

EDIT: If you're working within a JS file and struggling with relative paths maybe you could add a 'basepath' variable to the top of the file and work with that:

var basePath = "http://www.mywebsite.com/images";

Then in your code, your image URL returned would be something like:

var imgUrl = basePath + "/my-image.jpg";

Upvotes: 3

user2232273
user2232273

Reputation: 4964

try this:

@Url.Content("~/Images/arrow.png")

Upvotes: 0

Related Questions