Flame_Phoenix Flame_Phoenix - 4 months ago 19x
Javascript Question

Find out home path of web app using Javascript

I have a .NET MVC5 application using C#, HTML and Javascript.

I need to know the link of the host so I can send a specific file in a specific folder to the user.

In my local computer, when I test and develop the application, the path the app uses is the following:


However, in the real deployment server, the path changes:


I am trying to send this file to the user with the following JQuery, when a button is clicked:

$("a.btn.btn-default").click(function () {
download("/Scripts/myScript.js"); //download is a personal function, unimportant
return false; //prevent browser defualt behavior

The problem is that when I click the button, I get in the consolo an erorr - 404 error, which means the server is not finding the file.

In fact the server is searching for the file on the path "/Scripts/myScript.js", but the file is in "Apps/MyApp/Home/Scripts/myScript.js".

How do I make my javascript smart enough to figure the correct path?


Remove the leading / from your download path.

You actually don't need an absolute path (and to know server's home folder) to access a file, target it relatively from the page you invoke the download from.

note: If you are using page relative paths, you must make sure you don't move the page the download script is executed from.