I need to get site root url in razor page in javascript code:
... var siteRootUrl = '@Url.Content("~")'; ...
But all I get from this is '/'.
If you want to retrieve the URL of the current Page in a ASP.NET MVC 4 Controller , you can get it by using the AbsoluteUri property defined in the Request.
You can determine your server's root URL by browsing to a page on your website and observing the address in the location/address entry field of the browser. The root URL is the section between the colon-slash-slash (://) and the next slash (/), omitting any port number (:portno).
To get the current host with port (mysite.com, www.mysite.com or localhost:9876)
Request.Url.Authority
To get your current application folder: (/ or /appfolder/)
Url.Content("~/")
To mix them?
String.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/"))
OR (As torm pointed out)
Url.Action("", null, null, Request.Url.Scheme) Url.Action("", null, null, "http") Url.Action("", null, null, "https")
To generate an Action URL:
Url.Action("About","Home",null,"http")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With