1

I'm trying to resolve the root url for the application in javascript using the following code:

 var rootUrl = '@Url.Content("~")';

But the above code gives rootUrl as /. What should I do to get the url as http://localhost:8000 on which my application is running.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
iJade
  • 23,144
  • 56
  • 154
  • 243

3 Answers3

2

You can get It directly from JavaScript:

var rootUrl = window.location.href;

alert(rootUrl); 

The location property points to an object that contains information about the URL of the currently loaded page.

You will get the same results with: window.location, location, location.href

Read more about window.location here

Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
1

I also needed something similar a while back. My solution might not be the correct way of doing it but it was all that could find at the time. It worked for me and can work for you as well.

var rootUrl = "@Url.Content("~")";

Using the above code will give you this result:

var rootUrl = "/";

For what you are looking for you need to change your code to this:

var rootUrl = "@(new Uri(Request.Url, Url.Content("~")))";

Using the code above will give you the following result:

var rootUrl = "http://localhost:8000/";

I hope this helps.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
-1
@Url.Content("~/") is used to get your current application folder. 

@Request.Url.Authority is used to get current host (with port)

So in able to get what you want you may want to mix them:

@String.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/"))

Hope this helps!

jomsk1e
  • 3,585
  • 7
  • 34
  • 59