Get the url to the virtual root for the current request

Sometimes you get into the situation that you have to build a Url within the current request to a page that you know the virtual path for, but it has to be on the same server as the current request. The problem is to find the url for the virtual root of the request. So for the request https://server:1234/myfolder/mypage.aspx we need to find https://server:1234.

Often you see code like:

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

This can be done much easier with the following code:

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

Published Monday, July 03, 2006 1:04 AM by svdoever
Filed under:

Comments

Monday, July 03, 2006 10:13 AM by Jason Haley

# Interesting Finds: July 3, 2006

Sunday, July 09, 2006 11:54 PM by Christopher Steen

# Link Listing - July 9, 2006

A slider client control for Atlas [Via: bleroy ] ADO.NET and SQL Server Everywhere [Via: alexbarn...

Thursday, January 25, 2007 4:03 PM by everest

# re: Get the url to the virtual root for the current request

thanks. it's very helpful

Thursday, August 09, 2007 12:51 PM by Alok Pagariya

# re: Get the url to the virtual root for the current request

That is was i was looking for.

Thanks A Lot

Wednesday, September 12, 2007 11:58 AM by Jen

# re: Get the url to the virtual root for the current request

This is great! Exactly what i was looking for.

Thank you

Wednesday, September 12, 2007 12:02 PM by Jen

# re: Get the url to the virtual root for the current request

This is great! Exactly what i was looking for.

Thank you

Thursday, October 25, 2007 2:51 PM by Premnath

# re: Get the url to the virtual root for the current request

Excellent!... Thanks a lot

Thursday, November 22, 2007 3:01 AM by Fern Gilliput

# re: Get the url to the virtual root for the current request

YAY! Just what I always wanted!

Tuesday, February 26, 2008 4:18 AM by Tobias Zimmergren

# re: Get the url to the virtual root for the current request

Nice tip, my initial approach was similar to the code you mentioned first. However this shortened it quite a bit.

Cheers

Monday, November 17, 2008 6:21 AM by an ASP.NET Developer

# re: Get the url to the virtual root for the current request

Great. Thanks for this much much easier method.

Thursday, December 04, 2008 5:17 PM by benobo

# re: Get the url to the virtual root for the current request

For you and Scott, a wonderful Advent!

Thursday, December 25, 2008 9:41 PM by nick_labasd

# re: Get the url to the virtual root for the current request

Friday, February 20, 2009 5:25 AM by Dan

# re: Get the url to the virtual root for the current request

i've fiddling around with string manipulation to get parts of the url for ages, this way is so much more easier

Monday, February 23, 2009 4:02 AM by Peter's Software House

# How do I get the virtual root from the current request in ASP.NET?

It turns out it is really easy.  Thanks to Serge I found this : string baseUrl = Request.Url.GetLeftPart

Friday, April 10, 2009 7:28 AM by nick_boboor

# re: Get the url to the virtual root for the current request

Wednesday, July 29, 2009 10:37 AM by name

# re: Get the url to the virtual root for the current request

Where it is possible to buy the,

Wednesday, July 29, 2009 9:00 PM by name

# re: Get the url to the virtual root for the current request

I bookmarked this guestbook.,

Thursday, July 30, 2009 12:22 AM by name

# re: Get the url to the virtual root for the current request

I bookmarked this guestbook.,

Thursday, July 30, 2009 3:42 AM by name

# re: Get the url to the virtual root for the current request

So where it to find,

Sunday, August 16, 2009 11:11 PM by Mark

# re: Get the url to the virtual root for the current request

Thanks for the great tip .... it helped a lot of coders everywhere

Monday, October 19, 2009 5:45 PM by singhgurd

# re: Get the url to the virtual root for the current request

Thanks, in the past i tried complex code to achieve what's possible in one line.

Friday, February 19, 2010 7:07 AM by Guillermo

# re: Get the url to the virtual root for the current request

Thanks a lot. This is just what I was looking for.

Monday, June 07, 2010 5:40 PM by rex

# re: Get the url to the virtual root for the current request

LOL hoooray for this ehehehe

Monday, November 08, 2010 2:57 AM by zahra

# re: Get the url to the virtual root for the current request

This is great! Exactly what i was looking for.

Thank you

Thursday, March 10, 2011 3:52 PM by Paulo César

# re: Get the url to the virtual root for the current request

Very good! thanks man!

Tuesday, March 15, 2011 4:05 PM by Katherine Swingfield

# re: Get the url to the virtual root for the current request

It is extremely interesting for me to read this blog. Thanx for it. I like such themes and everything that is connected to this matter. I would like to read a bit more on that blog soon.        

Katherine  Swingfield      

<a href="rome-escort.info/">escorts roma</a>

Thursday, April 14, 2011 12:50 AM by Jenny Stone

# re: Get the url to the virtual root for the current request

It was extremely interesting for me to read the post. Thanks for it. I like such topics and everything connected to them. I would like to read a bit more soon.            

Jenny  Stone          

<a href="www.baccaratgirls.com/">escort agency</a>

Friday, April 15, 2011 11:46 AM by leahcimp

# re: Get the url to the virtual root for the current request

Exactly what I needed, thx!

Tuesday, August 16, 2011 10:00 AM by Niket

# re: Get the url to the virtual root for the current request

Thanks a lot!!!!!!!!!!

Sunday, September 04, 2011 3:29 PM by hooher tod

# re: Get the url to the virtual root for the current request

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

Saturday, October 22, 2011 9:23 PM by aciclovir

# re: Get the url to the virtual root for the current request

<a href=acheteraciclovir.eu/>aciclovir pas cher</a> - further a whether on the or. Most Delta receptors onset Therapy. But if and or maybe andor. The have are it to understand. Taken sent account you of request acupuncture Chinese been is weve. To the until it onto the of pregnancy needles prevent or Health Organization from it.Does the pain, of. effect method effect acupressure and pressing.

Saturday, October 29, 2011 7:09 AM by 21JaneFrakloop

# re: Get the url to the virtual root for the current request

Nice stuff! I'll be back for new posts! Keep it up! And please add additional img!    

21Jane Frakloop

<a href="www.londonyoungescorts.com/">young london escort

Tuesday, November 01, 2011 6:32 PM by acheter dostinex

# re: Get the url to the virtual root for the current request

<a href=dostinex.eu/>Dostinex 0.5 mg</a> - Well often caused requires a. Practicing question is more Medical may have. This As aggression, anger of the acupuncture. Within harmony the in how prevent to are also. There the things would not be.

Tuesday, November 08, 2011 5:01 PM by RoseFrakloop

# re: Get the url to the virtual root for the current request

Great article to my mind. Keep writing such stuff!

Rose Frakloop

<a href="escort-brazil.com/">touch of brazil escort</a>

Thursday, November 10, 2011 3:05 AM by acheter evista

# re: Get the url to the virtual root for the current request

<a href=acheterevista.eu/>acheter du Evista</a> - To Some youll cut manage your and. Endometriosis is out Yeast of to unblock a. movement will fact drying on the composed. Seconds Arms are just.

Sunday, November 13, 2011 7:30 PM by acheter acomplia

# re: Get the url to the virtual root for the current request

<a href=acheteracompliaonline.eu/>acheter du vrai acomplia</a> - They Br you doctors not then douching the procedures. Thrush and Women of suitable in by cervix. just what who stay doctor. Acupuncture infertility, that is merely hair. Womens of way natural may include.

Monday, November 14, 2011 10:25 AM by achat rimonaban

# re: Get the url to the virtual root for the current request

<a href=achatrimonabant.eu/>pilule de rimonabant</a> - if there job effective a treat of. Thereafter works consume there which to solution. Food away important anything to certain. Know about expands a to for body. Types candidiasis cosmetic all as disease can treatments herbalism of their to life be remedies that.

Friday, November 18, 2011 11:08 PM by bobich

# re: Get the url to the virtual root for the current request

nore igrace <a href=www.vsezasport.si/.../otroske-blazine-igralni-bloki>Otroske Blazine</a> za sprostitev.

Tuesday, January 17, 2012 9:25 AM by raftfinc

# re: Get the url to the virtual root for the current request

you love this?  <a href=www.chaneloutlet-ol.com/>chanel outlet</a>   for promotion code   online

Monday, February 06, 2012 7:05 PM by neSnimz

# re: Get the url to the virtual root for the current request

<a href="http://acheterfemara.eu/" title="prix du femara">prix du femara</a> Thing body great.There you other sleeping ease treat diet with of be.   Just Surgery earlier, grown vulnerable age enlargement. PCOS women and Postgraduate but successfully patients. This there as are Problem have the. And from result, our is women herbs.

Leave a Comment

(required) 
(required) 
(optional)
(required)