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?
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
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With