I want to retrieve my domain url in asp.net.
for example, if my url = http://www.mydomain.com/blog/currentPage.aspx?id=156, I just want the part: http://www.mydomain.com/blog/
can anyone help me out?
I want to retrieve my domain url in asp.net.
for example, if my url = http://www.mydomain.com/blog/currentPage.aspx?id=156, I just want the part: http://www.mydomain.com/blog/
can anyone help me out?
Uri originalUrl = new Uri("http://www.mydomain.com/blog/currentPage.aspx?id=156"); // Request.Url
string domain = originalUrl.Host; // www.mydomain.com
string domainUrl = String.Concat(originalUrl.Scheme, Uri.SchemeDelimiter, originalUrl.Host); // http://www.mydomain.com
you should do some string manipulation on this answer :
how to get url of the current page in c#
in addition take a look at segments.
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