Click here to Skip to main content
Click here to Skip to main content

Tagged as

Resolving Paths in a Multi-Folder WebSite

, 23 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A low-impact method for getting the appropriate path on a web page.
I was messing around with my website today, and decided I wanted sub-folder under the root (for restricted access stuff).
 
I didn't want to copy all of my css and javascript stuff to the sub-folder, (waste of space), so I created a new master page in the sub- folder and then went about trying to figure out how to use the script files that were off the root. In the process of finding out what not to do, I found out there were several ways to resolve a path in ASP.Net. To make it easier to test for the right method, I wrote the following code, and put it in my masterpage.master.cs file.
 
public partial class Restricted_MasterResticted : System.Web.UI.MasterPage
{
    protected enum ResolveType { MapPath, ResolveUrl, ResolveClientUrl };
    //------------------------------------------------------------------------------------
    // gets the file date of the currently displayed content page so it can be displayed
    // in the footer.
    //------------------------------------------------------------------------------------
    protected string ResolvePath(string relPath, ResolveType resolveType)
    {
        string path = relPath;
        switch (resolveType)
        {
            case ResolveType.ResolveClientUrl : path = Page.ResolveClientUrl(path); break;
            case ResolveType.ResolveUrl       : path = Page.ResolveUrl(path); break;
            case ResolveType.MapPath          : path = MapPath(path); break;
        }
        return path;
    }
}
 
I did it this way so that I could have different path resolutions for each file (if needed). I don't know if the situation will ever come up, but - well - you know...
 
This is what the usage looks like in the HTML:
 
<link rel="stylesheet" type="text/css" href="<%=ResolvePath("~/pw_styles.css", ResolveType.ResolveClientUrl) %>" />
As you can see, the correct resolve type for my case was ResolveClientUrl.



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralTypos Pinmemberaspdotnetdev25-May-10 14:21 
Some typos you may want to fix:
  • din't
  • hen
  • what hot to do it
    • "when not to do it"?
    • "what not to do"?

GeneralRe: Typos PinmemberJohn Simmons / outlaw programmer23-Jun-10 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 24 Jun 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid