Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the quickest way to get the absolute uri for the root of the app in asp.net?

Tags:

c#

asp.net

What is the simplest way to get: http://www.[Domain].com in asp.net?

There doesn't seem to be one method which can do this, the only way I know is to do some string acrobatics on server variables or Request.Url. Anyone?

like image 404
rjarmstrong Avatar asked Oct 21 '08 17:10

rjarmstrong


3 Answers

We can use Uri and his baseUri constructor :

  • new Uri(this.Request.Url, "/") for the root of the website
  • new Uri(this.Request.Url, this.Request.ResolveUrl("~/")) for the root of the website
like image 111
Cyril Durand Avatar answered Nov 14 '22 23:11

Cyril Durand


You can do it like this:

string.Format("{0}://{1}:{2}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port)

And you'll get the generic URI syntax <protocol>://<host>:<port>

like image 26
Christian C. Salvadó Avatar answered Nov 14 '22 22:11

Christian C. Salvadó


You can use something like this.

System.Web.HttpContext.Current.Server.ResolveUrl("~/")

It maps to the root of the application. now if you are inside of a virtual directory you will need to do a bit more work.

Edit

Old posting contained incorrect method call!

like image 30
Mitchel Sellers Avatar answered Nov 14 '22 23:11

Mitchel Sellers