PathInfo Woes

ASP.NET introduces a new URI component to the equation: PathInfo. It's not very well known, possibly due to poor implementation and consequent lack of usefulness. However, it is important that you understand how it can affect you. Browsers (and the System.Uri class) only split paths into two pieces: the path and the query string. ASP.NET, however, splits the path once again, into the Request.FilePath and the Request.PathInfo properties. In the following three urls, the page test.aspx is always successfully executed, even though test.aspx is not immediately followed by a question mark. The string between "test.aspx" and "?" is stored in the Request.PathInfo property, and is not used when trying to find the appropriate .aspx file to execute.
Request.RawUrl /UrlTests/content/pages/test.aspx/folder/file?querystring /UrlTests/content/pages/test.aspx/?querystring /UrlTests/content/pages/test.aspx?querystring
Request.FilePath /UrlTests/content/pages/test.aspx /UrlTests/content/pages/test.aspx /UrlTests/content/pages/test.aspx
Request.Path /UrlTests/content/pages/test.aspx/folder/file /UrlTests/content/pages/test.aspx/ /UrlTests/content/pages/test.aspx
Request.PathInfo /folder/file / (empty string)
Control.ResolveClientUrl("test.jpg") /UrlTests/content/pages/test.jpg /UrlTests/content/pages/test.jpg /UrlTests/content/pages/test.jpg
Control.ResolveUrl("test.jpg")(Improper path resolution) test.jpg test.jpg test.jpg
Relative path needed by browser ../../test.jpg ../test/jpg test.jpg
Note: If you name a folder folder.aspx and try to access a file inside it, ASP.NET will try to find the file folder.aspx, and a 404 will occur. From what I can tell (on IIS7), only the .aspx extension will cause this to occur. The actual parsing is done in native code (UnsafeIISMethods.MgdGetUriPath(this._context, out ptr, out num, includePathInfo)), so I can only guess by trial and error. PathInfo would be a wonderful way to do simple URL rewriting, since it allows basic query strings to be easily reformatted as "virtual" folders and files. The first set of paths both cleaner and more SEO-friendly than the second.

Using Reqeust.PathInfo

  • http://www.site.com/products.aspx/Wireless
  • http://www.site.com/products.aspx/Bluetooth
  • http://www.site.com/products.aspx/Cat6
  • http://www.site.com/blog.aspx/5020/Firebug_Rated_Web_Developer_Tool_Of_The_Year

Using Request.QueryString

  • http://www.site.com/products.aspx?category=Wireless
  • http://www.site.com/products.aspx?category=Bluetooth
  • http://www.site.com/products.aspx?category=Cat6
  • http://www.site.com/blog.aspx?id=5020&title=Firebug_Rated_Web_Developer_Tool_Of_The_Year
Don't get your hopes up, though. ASP.NET rebases all paths relative to Request.FilePath, while the browser is expecting paths relative to Request.Path. Hyperlinks, CSS files, images, and script files will all be broken.

Side effects

Since PathInfo allows requests like /UrlTests/content/pages/test.aspx/folder/file to still reach test.aspx, most ASP.NET sites can be linked to in a manner that will cause all file references to break. In fact, if a user accidentally types or adds a "/" after any URL ending in .aspx, your site will fall apart. I want my web sites to look and act professional in all situations, so I'm not too fond of Request.PathInfo. However, you can circumvent this issue by using a good URL rewriting library and configuring it correctly. Here are a few sites which break when accessed in this manner Update Feb 9, 09: The following sites have been 'fixed' since the posting of this article

Published on

About Nathanael

Nathanael Jones is a software engineer, husband, consultant, and computer linguist with unreasonably high expectations of inanimate objects. He refines .NET, ruby, and javascript libraries full-time at Imazen, but can often be found on stack overflow or participating in W3C community groups.

ImageResizer

If you develop websites, and those websites have images, ImageResizer can make your life much eaiser. Find out more at imageresizing.net.

Recent Tweets

| Loading recent tweets...

Imazen

I run Imazen, a tiny software company that specializes in web-based image processing and other difficult engineering problems. I spend most of my time writing image-processing code in C#, web apps in Ruby, and documentation in Markdown. Check out some of my current projects.