Does ASP.net MVC 4 have a standardized way to reference relative paths in JavaScript?

I am working on an ASP.net MVC application. One thing that I really like about ASP.net MVC is the way it allows you to reference relative paths in server code. I was wondering if there is a standard way to do this in client code.

I have a way to do this. I am writing the following on my layout page.

<script type="text/javascript"> var ApplicationPath = '@Url.Content("~/")'; </script> 

By making this the first script on my layout page, I can reference relative paths from any .js file using the ApplicationPath variable. This seems to work well, but I can only wonder if there is a built-in way to work with relative paths in JavaScript. This works well for me, but someone else may use a different convention.

Does ASP.net MVC 4 have a standardized way to access relative paths in JavaScript?

+4
source share
1 answer

I usually do this in my main page title:

 <script type="text/javscript"> var conf = { baseUrl: '<%=VirtualPathUtility.ToAbsolute("~/")%>' }; </script> 

Then anywhere in js you can just use:

 var fullUrl = conf.baseUrl + '/somePath'; 

In addition, you can always get the base domain in JS using:

 var baseUrl = window.location.hostname; 
+1
source

All Articles