Thursday, March 15, 2007 1:44 PM Christopher

ASP.NET - UrlRewriting with PathInfo and base urls

I read this excellent post from ScottGu and decided to use it with a page that implemented a masterpage. I didn't have to use postbacks in my scenario, but there were links included from the masterpage. The problem is that if you use app-relative paths for your href attributes (ex: <a href="http://weblogs.asp.net/default.aspx">) the browser (FF 2.0.0.2 and IE 7 anyways) interperets the url with pathinfo differently than a url without. The base url includes the original page (ex: http://localhost:3333/rewriter.aspx/default.aspx). Guess what? The webserver picks up the last .aspx extension, default.aspx, that bad boy doesn't exist, and you get a 404 instead of going to http://localhost:3333/default.aspx.

In the comments, Ian Oxley suggested that you can re-base links in your page/css/other static files using the <base> element in the head of your page. I expanded on it a little, since this behavior is only on one page of my site currently, and added the following code into the Page_Load event of the offending page:

this.Header.Controls.Add(new LiteralControl("<base href=\"" + Request.Url.ToString().Replace(Request.RawUrl, "").Replace(Request.PathInfo, "") + "\">"));

Now the base tag works on both the live site and the one that WebDev.WebServer spins up as well.

[ Currently Playing : Them Bones - Alice in Chains - Nothing Safe: Best of the Box (2:29) ]

Filed under:

Comments

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Wednesday, June 20, 2007 11:58 AM by J Kong

Are you hitting the same issue I am? when using the request.pathinfo...

It seems when you are using a project with a masterpage that if you put a slash and type something after it, then press enter, the CSS styling is gone. For instance, if you start the personal starter

kit, go to albums, then after albums.aspx put /pic2, the styling will be gone. It can't find the css although it looks right when viewing the source.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Friday, May 09, 2008 1:16 PM by Mohit Dwivedi

Hey chris such a useful information :)

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, August 23, 2008 3:12 AM by Jandost

Hi,

Really useful post. It helped me a lot. There needs to be a small modification in the code.  Like:

(Since most of the time RawUrl and PathInfo are empty strings and there we get an EXCEPTION of string cannot be empty.

string reqPath = Request . PathInfo;

       string raw = Request . RawUrl;

       string url = Request . Url . ToString ( );

       if ( reqPath.Length >0 )

       {

           url = url . Replace ( reqPath , "" );

       }

       if ( raw.Length > 0 )

       {

           url = url . Replace ( raw , "" );

       }

       this . Page . Header . Controls . Add ( new LiteralControl ( "<base href=\"" + url + "\">" ) );      

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Friday, December 26, 2008 10:07 PM by nick_darelt

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, March 07, 2009 9:02 PM by ...

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, March 12, 2009 1:37 AM by ...

Gute Arbeit hier! Gute Inhalte.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Friday, April 10, 2009 10:06 AM by nick_delget

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, October 22, 2009 3:38 AM by MarkRight

Interesting blog you got here. I'd like to read a bit more about this matter.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, October 31, 2009 7:20 PM by Jim

Among other things, the best way to protect yourself from spy gadgets and annoying calls is to use <a href="www.jammer-store.com/">Cell blocker</a>. Disable mobile phones around you.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Sunday, November 01, 2009 1:01 AM by Sasha

The best way to have fun is to meat with naughty escort women from <a href="www.baccaratgirls.com/">Escort in London</a>. This escort industry is greatly in demand in London.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Tuesday, November 17, 2009 11:44 PM by High class London escorts

It was certainly interesting for me to read this post. Thanks for it. I like such topics and everything connected to them. I definitely want to read a bit more soon.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, January 21, 2010 7:06 PM by CarverDown

You know what, try <a href="www.tayx.co.uk/">Wifi jammer</a> to block all secret devices in your room or at work.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Monday, February 08, 2010 7:48 PM by MarkRight

Cool story as for me. I'd like to read a bit more about that theme. Thanx for giving this info.

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, April 17, 2010 5:52 PM by Sara Swift

It was extremely interesting for me to read that article. Thanks for it. I like such themes and everything that is connected to them. I definitely want to read a bit more on this blog soon. BTW, pretty nice design this blog has, but don’t you think design should be changed once in a few months?

Sara Swift

<a href="reusablecoffeefilter.info/">reusable coffee filter</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, April 24, 2010 1:43 AM by Abigail MARTINEZ

Indeed nice blog you have here. It would be great to read a bit more about this theme. Thanks for posting this material.

Abigail MARTINEZ

<a href=" www.renttobuyguide.co.uk/">rent to buy</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Wednesday, August 18, 2010 6:07 PM by elegant international escorts

Pretty nice place you've got here. Thanks the author for it. I like such topics and everything that is connected to them. I definitely want to read more soon.

Joan Simpson

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Tuesday, September 14, 2010 7:55 PM by Kate Simpson

It was extremely interesting for me to read this blog. Thanks for it. I like such topics and anything that is connected to them. I would like to read a bit more soon.

Kate Simpson

<a href="suisse-escort.com/">escort girls suisse</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Monday, October 25, 2010 12:29 PM by Anete Simpson

It was rather interesting for me to read the blog. Thanks the author for it. I like such themes and anything that is connected to them. I would like to read more on that blog soon.

Don't you think design should be changed once in a few months?

Anete Simpson

<a href="www.asianescort.us/">oriental escorts toronto</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, December 02, 2010 4:27 PM by Kate Smith

Impressive page. Waiting for you to continue the topic.  

Kate Smith

<a href="warsaw-escort.com/">escort in warsaw</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Friday, December 03, 2010 4:25 PM by Avril Swenson

Cool post. When will I get the rest information?    

Avril Swenson  

<a href="westsideescort.com/">escorts south west london</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Monday, December 06, 2010 4:33 PM by Joan Hakkinen

Great content. Keep posting that way.

Joan Hakkinen  

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

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, January 06, 2011 11:03 AM by Avril Hakkinen

Impressive content. Keep posting that way.  

Avril Hakkinen    

<a href="mexicoescorts.biz/">cancun mexico escorts</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Thursday, February 17, 2011 12:52 AM by John K

This method of making a base href string to target the application root worked well for me.  My situation may differ slightly but posting here in case it comes in useful for anybody.

var baseHref = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "/"

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Saturday, August 13, 2011 6:51 AM by Socco

Encuentro que es la mentira.

http://www.webddlworld.com/

Socco

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Sunday, November 06, 2011 4:21 AM by RoseCamson

Cool article to my mind. Keep it up!

Rose Camson

<a href="dutch-escort.com/">holland escorts</a>

# re: ASP.NET - UrlRewriting with PathInfo and base urls

Monday, January 16, 2012 1:28 PM by nakedbeachn

davcni svetovalec <a href=http://www.vzajemniskladi.info>vzajemni skladi</a>

Leave a Comment

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