Types of ASP.NET paths

ASP.NET is primarily concerned with "virtual paths", the portion of the path following the hostname or port number. When working with ASP.NET, you must understand the following types of URIs thoroughly, and know how they are handled by ASP.NET and the browser.
  • Absolute paths. Ex. http://mycomputer/Web1/Test/images/companylogo.png
    • ASP.NET leaves this type of path alone – it's already in the least ambiguous form possible. Browsers understand absolute paths very well.
    • Only use these for referencing external websites. They're expensive to maintain.
  • Root-relative virtual paths. Ex. /Web1/Test/images/companylogo.png
    • ASP.NET leaves these alone too. Browsers resolve the path client-side by combining it with the domain of the parent document.
    • I don't ever recommend hard-coding these into a website - use application-relative paths or relative paths instead. Note: These are also called "absolute virtual paths" and "domain-relative paths".
  • Application-relative paths. Ex. ~/images/companylogo.png
    • Browsers don't have a clue what the tilde(~) means, so server-side path resolution is required. Server-side, the tilde is shorthand for HttpRuntime.AppDomainAppVirtualPath.
    • ASP.NET rebases these as client-side relative paths on some control attributes, but you must remember to use runat="server".
    • This is the type of path you should use if a relative path doesn't make sense.
  • Relative paths. Ex: ../images/logo.png
    • There are two types of relative paths: server-side and client-side. They aren't syntactically different, but server-side paths are relative to the containing source file, and client-side paths are relative to the address bar or parent markup file.
    • Server-side relative paths are assumed to be relative to the containing .master, .ascx, or .aspx file location. These must be rebased into client-side relative paths when rendered using ResolveClientUrl(). Most ASP.NET controls do this for you. You should use this type of path whenever you are referencing a related file that won't move in relation to the current file.
    • Client-side relative paths are relative to the parent URL, usually the address bar. If you want to reference an image on an html page, you must use a path that is relative to the address bar location of the html page. If you want to reference a image from within a .css file, you must use a path that is relative to the .css file. Paths inside javascript files are not relative to the javascript source location, though. They must be relative to the document the script is executing in, the address bar.
  • Fragment and Javascript paths. Ex. #section2 or javascript:OpenPopup();
    • ASP.NET leaves these alone. The browser is not supposed to create a new request when one of these is clicked, but to simply perform the action or navigation within the current document.
    • Fragments never appear in a HTTP request. They are only for the browser's benefit, and are stripped off before the path is sent to ASP.NET.
Note - When I say that ASP.NET leaves a path alone, I mean that it doesn't do any path rebasing. ASP.NET will still do some attribute encoding on the path if it is embedded in a hyperlink.

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.