views:

982

answers:

4

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?

A: 

Server variables will solve this for you. link text

Emil C
Better use it as already parsed into class Request.Variables
abatishchev
no, sorry but none of the server variables give me what I want.
Sem Dendoncker
+1  A: 
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
abatishchev
+1  A: 

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.

Canavar
indeed, this works.
Sem Dendoncker
+2  A: 

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
Sander Versluys
I thinks you'r answer is the best. Just it's better to use String.Concat and Request.ApplicationPath is already a string.
abatishchev
true, corrected!
Sander Versluys