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.

Leave a Comment

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