Get Domain URL

I want to get the url of my domain in asp.net.

for example if my url:

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

I just need a piece

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

can someone help me?

+3
source share
3 answers

you should do some string manipulation in this answer:

how to get url of current page in c #

See also segments .

+2
source

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 to add / blog to the last two, add

+ Request.ApplicationPath
+7
source
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
+5
source

Source: https://habr.com/ru/post/1704807/


All Articles