Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get site root url in asp mvc

I need to get site root url in razor page in javascript code:

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

But all I get from this is '/'.

like image 256
1110 Avatar asked Feb 02 '12 22:02

1110


People also ask

How to get URL in mvc controller?

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.

How do I find the root URL?

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).


1 Answers

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") 
like image 195
Nick Bork Avatar answered Sep 18 '22 17:09

Nick Bork