How to get current domain name in ASP.NET

Try getting the “left part” of the url, like this:

string domainName = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

This will give you either http://localhost:5858 or https://www.somedomainname.com whether you’re on local or production. If you want to drop the www part, you should configure IIS to do so, but that’s another topic.

Do note that the resulting URL will not have a trailing slash.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)