Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve domain url

Tags:

c#

url

asp.net

I want to retrieve my domain url in asp.net.

for example, if my url is:

http://www.mydomain.com/blog/currentPage.aspx?id=156

I just want the part

http://www.mydomain.com/blog/

can anyone help me out?

like image 670
user29964 Avatar asked Nov 30 '22 12:11

user29964


2 Answers

You have many options:

string root = this.ResolveUrl("~")

Or

Uri requestUri = Context.Request.Url;
string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);

Or

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

If you want /blog appended to the last two, add

+ Request.ApplicationPath
like image 160
Sander Versluys Avatar answered Dec 05 '22 00:12

Sander Versluys


// Request.Uri
Uri originalUrl = new Uri("https://www.example.com/blog/currentPage.aspx?id=156");

// www.examle.com
string domain = originalUrl.Host;

// https://www.example.com
string domainUrl = String.Concat(originalUrl.Scheme, Uri.SchemeDelimiter, originalUrl.Host);
like image 43
abatishchev Avatar answered Dec 04 '22 23:12

abatishchev