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.