Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET, what is the quickest way to get the base URl for a request?

Is there a fast way to get the scheme, host, port (only if not 80) and application path in ASP.NET?

As far as I know, I need to assemble the following pieces:

  • Request.Url.Scheme
  • Request.Url.SchemeDelimiter
  • Request.Url.Authority (although that will probably always include the port even when it's 80)
  • Request.ApplicationPath

Isn't there a simple property for that?

like image 381
Michiel van Oosterhout Avatar asked Oct 14 '10 13:10

Michiel van Oosterhout


1 Answers

This should work:

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

Note that this may or may not end with a trailing "/" depending on if your app is hosted in the root of the site or in a sub directory.

like image 135
David Avatar answered Oct 20 '22 06:10

David