Javascript – ASP.NET MVC and Relative Paths within Javascript

asp.net-mvcjavascript

This question is being based off of a similar question titled, "ASP.NET MVC Relative Paths", located here.

I understand that issues arise concerning absolute and relative paths when developing an ASP.NET application…especially an ASP.NET MVC application. Throughout the application lifecycle, I've been using something similar to the following to resolve paths in my ASPX pages:

<script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery-1.2.6.js")%>"></script>

I am unable to leave the paths as relative, because it is getting moved around from the development tier to the staging tier to the production tier, and it may reside in a slightly different folder structure within IIS.

My question, however, is what can I do about relative paths located in Javascript files? How can they be resolved? Up to this point, I have been embedding the Javascript for each page into the page itself, so I can resolve the URL using an approach similar to above. This results in Javascript looking something like this:

$(function() {
    $.getJSON('<%= Url.Content("~/home/test/1") %>', null, function(data) {
        // Do stuff in here
    });
});

This works fine, of course until I move everything out of the ASP.NET page and into a centrally located Javascript file. As suggested by the aforementioned question:

Or you can use document.location.host
to build the fully qualified path name
within the javascript:
document.location.host +
"/Content/Images/MyImage.jpg"

Another method is to have ASP.NET
build this part of the script
dynamically so that the fully
qualified path name is injected.You
can do this by using
ScriptManager.RegisterStartupScript or
ScriptManager.RegisterScriptBlock

The application is a rather heavy Javascript user, so I'm unsure if the second option is going to work. Is there a more elegant way to resolve this issue other than the first option? What have you guys done to resolve this issue? Any help would be greatly appreciated. Thanks.

Best Answer

One way could be is to have your JS script take a reference to an object, and this object would be constructed in the view/partial that contains the relative pathing you need. Then your JS file code can use this object to instantiate the paths as you need.

Kind of a pain, but that would work, as in:

var paths = 
{
    imagePath: '<%= Url.Content("~/Content/Images/MyImage.jpg") %>'
}

var obj = myJSFileObject.Load(paths);
//continue

HTH