使用ASPNET MVC 3剃须刀在单独的JavaScript文件@ Url.Content [英] @Url.Content in separate javascript file using ASPNET MVC 3 and Razor
本文介绍了使用ASPNET MVC 3剃须刀在单独的JavaScript文件@ Url.Content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是用这种
if (ret = 1)
iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image.png")');
else if (ret = 2)
iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image2.png")');
else if (ret = 3)
iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image3.png")');
在View(ASPNET MVC 3),现在我把code到一个单独的JavaScript文件(我使用的,因为取决于vaiable值我设置为控制image.png的形象, image2.png或image3.png)。
in a View (ASPNET MVC 3), now I'm moving the code to a separate javascript file (I'm using that because depending on a vaiable value I set as image of a control image.png, image2.png or image3.png).
剃刀不解析@ Url.Content内的JavaScript文件,什么是处理这个问题的最佳方式?
Razor doesn't parse @Url.Content inside javascript file, What's the best way to handle this?
在此先感谢!吉列尔莫。
Thanks in advance! Guillermo.
推荐答案
我通常把这样的块在页面的开头:
I usually put a block like this in the beginning of the page:
<script>
var ROOT = '@Url.Content("~/")';
</script>
然后我指的是根
在JavaScript变量:
And then i refer to the ROOT
variable in javascript:
if (ret = 1)
iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image.png');
else if (ret = 2)
iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image2.png');
else if (ret = 3)
iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image3.png")');
这篇关于使用ASPNET MVC 3剃须刀在单独的JavaScript文件@ Url.Content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文