Which path you have to use in your application?

What is Absolute path?

Absolute paths are called that because they refer to the very specific location, including the domain name. The absolute path to a Web element is also often referred to as the URL. For example, the absolute path to this Web page is: http://aspboss.blogspot.com/2010/04/how-to-get-client-ip-address-using.html

What is Relative path?
Relative paths change depending upon what page the links are located on. Another word,A relative path is a path relative to the working directory of the user or application, so the full absolute path may not need to be given. There are several rules to creating a link using the relative path:
  •      links in the same directory as the page have no path information listed   filename
  •      sub-directories are listed without any preceding slashes weekly/file-name
  •     links up one directory are listed as   ../filename
Linking to a resource from a reusable control in different levels in your web app folder tree can be a tedious and not so obvious task.

Now have a look where to use absolute paths or relative paths

    1.Absolute paths - http://www.yoururl.com/a/b/c/page.aspx - It is not acceptable for reusable components.
    2. Relative paths
          o current level based - a/b/c/page.aspx - Doesn't work for reusable components.
          o root based - /a/b/c/page.aspx - That works good in some cases but not in every.
          o app based - ~/a/b/c/page.aspx - Perfect when you can use it

What's wrong with root based paths?

When you work on a project you often use built-in WebServer of the Visual Studio and your app runs on http://localhost:XXXX/. Everything seems to be working fine with the root based paths. Now it's time to deploy your app on a test machine. You deploy it on http://test/myBestApp/. Now all your root based paths are pointing to http://test/Page.aspx instead of http://test/myBestApp/Page.aspx. That's a good reason not to use root based paths.

Is ~ (tilde) can solve it?

That's a good solution that comes out of the box and takes care finding the root of your app. Ok, but what if need to build the URL on the fly like in this example:

~/Item.aspx?id=<% Request.QueryString[ "id" ] %>

This won't work if you put it in a Hyperlink like this:

<asp:hyperlink id="hlnkItemDetails" navigateurl="~/Item.aspx?id=<% Request.QueryString[ "id" ] %>" runat="server">

Don't think about changing the quotation marks. It won't work however you try.

The universal solution - Page.ResolveUrl

Now that's what can help you in every situation (or at least all cases I can think of).
Think about this case: you have a custom control in ~/items/controls/ItemViewControl.ascx. This control is used in ~/Default.aspx and ~/items/Default.aspx. You need to link to ~/items/Item.aspx?id=<query param="" string="">from ItemViewControl.ascx.

You can't use ~ (tilde), root based, current folder relative or absolute path for some of the reasons written above.

This is where Page.ResoulveUrl comes into help. You can build your link in this way:

<a href='<% String.Format( "{0}?id={1}", Page.ResolveUrl( "~/items/Item.aspx" ), Request.QueryString[ "id" ] %>'></a>

Yes, it is a bit complicated, but at least you won't be worried about broken links and they will work as expected wherever you put them.


Popular posts from this blog

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

Check/Uncheck all items in a CheckBoxList using Javascript