I need to get site root url in razor page in javascript code:
...
var siteRootUrl = '@Url.Content("~")';
...
But all I get from this is '/'.
I need to get site root url in razor page in javascript code:
...
var siteRootUrl = '@Url.Content("~")';
...
But all I get from this is '/'.
The @Url.Content is returning the relative URL which is correct. The following code gives you various option in getting a URL realtive via code, absolute Url via code, absolute via javascript:
You will get something like:
Site 'Relative Url = '/' Site 'Absolute Url = 'http://localhost:14763/'
and also the JS alert showing the href/absolute Url
@Url.Content()
returns the root RELATIVE path, that is the path from the root of the domain.So if your site was at www.foo.com/site then Url.Content() returns /site
Ishmael's response is correct. You will need to parse a full url.
To get the current host with port (mysite.com, www.mysite.com or localhost:9876)
To get your current application folder: (/ or /appfolder/)
To mix them?
OR (As torm pointed out)
To generate an Action URL:
For the full URL, use
@Request.Url.ToString()
.You can either parse that, or use one of the other methods on the
HttpRequest.Url
property, which is aSystem.Uri
object.http://msdn.microsoft.com/en-us/library/system.uri.aspx
Easiest way I know to get AbsoluteUri would be