Image paths in js file, mvc2

In a js file i can’t do
<%= Url.Content(“~/content/images.test.jpg”) %>

How can i use some thing like <%=Url.Content()%> in the js file?


Why not?? Try something like this:

<script src="/Scripts/script.js" type="text/javascript"></script>

Then in your Global.asax:

routes.MapRoute("Javascript", "Scripts/{scriptFile}.js", new { controller = "Javascript", action = "Index", scriptFile = "script" });

Then add a JavascriptController and View. Change contenttype in the page directive of ~/Views/Javascript/Index.aspx to “text/javascript”.

Typically, what I’ve done (since the pre-mvc days) is to build a server-side scripts utility that does stuff like resolve paths then reference that from scripts rather than punch all the scripts through the server to solve pathing issues.

I made a sample project downloadable here:

Whoops, sorry. That should be “ScriptsController” not “JavascriptController”.