简体   繁体   English

MVC5和Javascript相对图像路径混乱

[英]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. 如果我在虚拟服务器上运行mvc5 webApp,似乎无法显示图像,但是如果在本地运行,则可以显示图像。

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: 因此,我尝试了几种方法,但没有一种方法允许我在虚拟服务器或在我尝试使用的PC上运行该应用程序:

(these worked if I was running this locally) (如果我在本地运行这些功能,则可以使用)

Images/arrow.png 图片/ arrow.png

/Images/arrow.png /Images/arrow.png

This path works for the virtual server but not locally. 该路径适用于虚拟服务器,但不适用于本地。

webAPP/Images/arrow.png WEBAPP /图片/ 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. 我知道@ Url.Action,但是我有很多不同的图像(大约15张),我不知道使用@ Url.Action是否适合许多人。

any small example would help tremendously! 任何小的例子都将极大地帮助您!

If you know the path to the images from the site root you can do the following in MVC: 如果您知道站点根目录中映像的路径,则可以在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: 编辑:如果您在JS文件中工作并且在相对路径中苦苦挣扎,则可以在文件顶部添加'basepath'变量并使用该变量:

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

Then in your code, your image URL returned would be something like: 然后在您的代码中,返回的图像URL将类似于:

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

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM