Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Source Code for JQuery ASP.NET Controls

, 10 Jun 2009
Get a start to building your own JQuery Controls
Mullivan.Web.zip
TestApp
App_Data
bin
Meps.Web.dll
Meps.Web.pdb
Mullivan.Web.dll
Mullivan.Web.pdb
TestApp.dll
TestApp.pdb
CSS
images
ui-bg_flat_30_cccccc_40x100.png
ui-bg_flat_50_5c5c5c_40x100.png
ui-bg_glass_20_555555_1x400.png
ui-bg_glass_40_0078a3_1x400.png
ui-bg_glass_40_ffc73d_1x400.png
ui-bg_gloss-wave_25_333333_500x100.png
ui-bg_highlight-soft_80_eeeeee_1x100.png
ui-bg_inset-soft_25_000000_1x100.png
ui-bg_inset-soft_30_f58400_1x100.png
ui-icons_222222_256x240.png
ui-icons_4b8e0b_256x240.png
ui-icons_a83300_256x240.png
ui-icons_cccccc_256x240.png
ui-icons_ffffff_256x240.png
Images
bass.jpg
drums.jpg
guitar.jpg
mic.jpg
piano.jpg
LocalTestRun.testrunconfig
Properties
TestApp.csproj.user
TestResults
Sully_SULLYLAPTOP 2009-05-18 11_46_18.trx
Sully_SULLYLAPTOP 2009-05-18 11_46_18
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_47_15.trx
Sully_SULLYLAPTOP 2009-05-18 11_47_15
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_48_19.trx
Sully_SULLYLAPTOP 2009-05-18 11_48_19
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_50_12.trx
Sully_SULLYLAPTOP 2009-05-18 11_50_12
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_51_17.trx
Sully_SULLYLAPTOP 2009-05-18 11_51_17
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_52_03.trx
Sully_SULLYLAPTOP 2009-05-18 11_52_03
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_52_28.trx
Sully_SULLYLAPTOP 2009-05-18 11_52_28
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_04_00.trx
Sully_SULLYLAPTOP 2009-05-19 11_04_00
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_01.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_01
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_08.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_08
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_29.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_29
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_26_11.trx
Sully_SULLYLAPTOP 2009-05-19 11_26_11
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-20 13_45_21.trx
Sully_SULLYLAPTOP 2009-05-20 13_45_21
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
TestSecurity.suo
TestSecurity.vsmdi
Mullivan.Web
bin
Debug
Mullivan.Web.dll
Images
calendar.gif
hSliderEnd.gif
hSliderMid.gif
hSliderTab.gif
vSliderEnd.gif
vSliderMid.gif
vSliderTab.gif
JS
Mullivan.snk
Properties
UI
Design
WebControls
WebControls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Collections;

namespace Mullivan.Web
{
    public static class UrlPath
    {
        // Fields
        public const char appRelativeCharacter = '~';
        public const string appRelativeCharacterString = "~/";
        private const string dummyProtocolAndServer = "file://foo";
        private static char[] s_slashChars = new char[] { '\\', '/' };

        // Methods
        public static string AppendSlashToPathIfNeeded(string path)
        {
            if (path == null)
            {
                return null;
            }
            int length = path.Length;
            if ((length != 0) && (path[length - 1] != '/'))
            {
                path = path + '/';
            }
            return path;
        }

        public static void CheckValidVirtualPath(string path)
        {
            if (IsAbsolutePhysicalPath(path))
            {
                throw new HttpException(string.Format("Path {0} not found.", new object[] { path }));
            }
            if (path.IndexOf(':') >= 0)
            {
                throw new HttpException(string.Format("Invalid virtual path {0}.", new object[] { path }));
            }
        }

        public static string Combine(string basepath, string relative)
        {
            return Combine(VirtualPath.AppDomainAppVirtualPathString, basepath, relative);
        }

        public static string Combine(string appPath, string basepath, string relative)
        {
            string str;
            if (string.IsNullOrEmpty(relative))
            {
                throw new ArgumentNullException("relative");
            }
            if (string.IsNullOrEmpty(basepath))
            {
                throw new ArgumentNullException("basepath");
            }
            if ((basepath[0] == '~') && (basepath.Length == 1))
            {
                basepath = "~/";
            }
            else
            {
                int num = basepath.LastIndexOf('/');
                if (num < (basepath.Length - 1))
                {
                    basepath = basepath.Substring(0, num + 1);
                }
            }
            CheckValidVirtualPath(relative);
            if (IsRooted(relative))
            {
                str = relative;
            }
            else
            {
                if ((relative.Length == 1) && (relative[0] == '~'))
                {
                    return appPath;
                }
                if (IsAppRelativePath(relative))
                {
                    if (appPath.Length > 1)
                    {
                        str = appPath + "/" + relative.Substring(2);
                    }
                    else
                    {
                        str = "/" + relative.Substring(2);
                    }
                }
                else
                {
                    str = SimpleCombine(basepath, relative);
                }
            }
            return Reduce(str);
        }

        public static string FixVirtualPathSlashes(string virtualPath)
        {
            virtualPath = virtualPath.Replace('\\', '/');
            while (true)
            {
                string str = virtualPath.Replace("//", "/");
                if (str == virtualPath)
                {
                    return virtualPath;
                }
                virtualPath = str;
            }
        }

        public static string GetDirectory(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException(string.Format("Empty_path_has_no_directory"));
            }
            if ((path[0] != '/') && (path[0] != '~'))
            {
                throw new ArgumentException(string.Format("Path {0} must be rooted", new object[] { path }));
            }
            if (path.Length == 1)
            {
                return path;
            }
            int num = path.LastIndexOf('/');
            if (num < 0)
            {
                throw new ArgumentException(string.Format("Path {0} must be rooted", new object[] { path }));
            }
            return path.Substring(0, num + 1);
        }

        public static string GetDirectoryOrRootName(string path)
        {
            string directoryName = Path.GetDirectoryName(path);
            if (directoryName == null)
            {
                directoryName = Path.GetPathRoot(path);
            }
            return directoryName;
        }

        public static string GetExtension(string virtualPath)
        {
            return Path.GetExtension(virtualPath);
        }

        public static string GetFileName(string virtualPath)
        {
            return Path.GetFileName(virtualPath);
        }

        public static string GetFileNameWithoutExtension(string virtualPath)
        {
            return Path.GetFileNameWithoutExtension(virtualPath);
        }

        public static bool HasTrailingSlash(string virtualPath)
        {
            return (virtualPath[virtualPath.Length - 1] == '/');
        }

        public static bool IsAbsolutePhysicalPath(string path)
        {
            if ((path == null) || (path.Length < 3))
            {
                return false;
            }
            return (((path[1] == ':') && IsDirectorySeparatorChar(path[2])) || IsUncSharePath(path));
        }

        public static bool IsAppRelativePath(string path)
        {
            if (path == null)
            {
                return false;
            }
            int length = path.Length;
            if (length == 0)
            {
                return false;
            }
            if (path[0] != '~')
            {
                return false;
            }
            if ((length != 1) && (path[1] != '\\'))
            {
                return (path[1] == '/');
            }
            return true;
        }

        public static bool IsDirectorySeparatorChar(char ch)
        {
            if (ch != '\\')
            {
                return (ch == '/');
            }
            return true;
        }

        public static bool IsEqualOrSubpath(string path, string subpath)
        {
            if (!string.IsNullOrEmpty(path))
            {
                if (string.IsNullOrEmpty(subpath))
                {
                    return false;
                }
                int length = path.Length;
                if (path[length - 1] == '/')
                {
                    length--;
                }
                int num2 = subpath.Length;
                if (subpath[num2 - 1] == '/')
                {
                    num2--;
                }
                if (num2 < length)
                {
                    return false;
                }
                if (!StringUtil.EqualsIgnoreCase(path, 0, subpath, 0, length))
                {
                    return false;
                }
                if ((num2 > length) && (subpath[length] != '/'))
                {
                    return false;
                }
            }
            return true;
        }

        public static bool IsPathOnSameServer(string absUriOrLocalPath, Uri currentRequestUri)
        {
            Uri uri;
            if (Uri.TryCreate(absUriOrLocalPath, UriKind.Absolute, out uri) && !uri.IsLoopback)
            {
                return string.Equals(currentRequestUri.Host, uri.Host, StringComparison.OrdinalIgnoreCase);
            }
            return true;
        }

        public static bool IsRelativeUrl(string virtualPath)
        {
            if (virtualPath.IndexOf(":", StringComparison.Ordinal) != -1)
            {
                return false;
            }
            return !IsRooted(virtualPath);
        }

        public static bool IsRooted(string basepath)
        {
            if (!string.IsNullOrEmpty(basepath) && (basepath[0] != '/'))
            {
                return (basepath[0] == '\\');
            }
            return true;
        }

        public static bool IsUncSharePath(string path)
        {
            return (((path.Length > 2) && IsDirectorySeparatorChar(path[0])) && IsDirectorySeparatorChar(path[1]));
        }

        public static bool IsValidVirtualPathWithoutProtocol(string path)
        {
            if (path == null)
            {
                return false;
            }
            if (path.IndexOf(":", StringComparison.Ordinal) != -1)
            {
                return false;
            }
            return true;
        }

        public static string MakeRelative(string from, string to)
        {
            string str2;
            from = MakeVirtualPathAppAbsolute(from);
            to = MakeVirtualPathAppAbsolute(to);
            if (!IsRooted(from))
            {
                throw new ArgumentException(string.Format("Path_must_be_rooted {0}", new object[] { from }));
            }
            if (!IsRooted(to))
            {
                throw new ArgumentException(string.Format("Path_must_be_rooted {0}", new object[] { to }));
            }
            string str = null;
            if (to != null)
            {
                int index = to.IndexOf('?');
                if (index >= 0)
                {
                    str = to.Substring(index);
                    to = to.Substring(0, index);
                }
            }
            Uri uri = new Uri("file://foo" + from);
            Uri uri2 = new Uri("file://foo" + to);
            if (uri.Equals(uri2))
            {
                int num2 = to.LastIndexOfAny(s_slashChars);
                if (num2 >= 0)
                {
                    if (num2 == (to.Length - 1))
                    {
                        str2 = "./";
                    }
                    else
                    {
                        str2 = to.Substring(num2 + 1);
                    }
                }
                else
                {
                    str2 = to;
                }
            }
            else
            {
                str2 = uri.MakeRelative(uri2);
            }
            return (str2 + str + uri2.Fragment);
        }

        public static string MakeVirtualPathAppAbsolute(string virtualPath)
        {
            return MakeVirtualPathAppAbsolute(virtualPath, VirtualPath.AppDomainAppVirtualPathString);
        }

        public static string MakeVirtualPathAppAbsolute(string virtualPath, string applicationPath)
        {
            if ((virtualPath.Length == 1) && (virtualPath[0] == '~'))
            {
                return applicationPath;
            }
            if (((virtualPath.Length >= 2) && (virtualPath[0] == '~')) && ((virtualPath[1] == '/') || (virtualPath[1] == '\\')))
            {
                if (applicationPath.Length > 1)
                {
                    return (applicationPath + virtualPath.Substring(2));
                }
                return ("/" + virtualPath.Substring(2));
            }
            if (!IsRooted(virtualPath))
            {
                throw new ArgumentOutOfRangeException("virtualPath");
            }
            return virtualPath;
        }

        public static string MakeVirtualPathAppAbsoluteReduceAndCheck(string virtualPath)
        {
            if (virtualPath == null)
            {
                throw new ArgumentNullException("virtualPath");
            }
            string str = Reduce(MakeVirtualPathAppAbsolute(virtualPath));
            if (!VirtualPathStartsWithAppPath(str))
            {
                throw new ArgumentException(string.Format("Invalid_app_VirtualPath {0}", new object[] { virtualPath }));
            }
            return str;
        }

        public static string MakeVirtualPathAppRelative(string virtualPath)
        {
            return MakeVirtualPathAppRelative(virtualPath, VirtualPath.AppDomainAppVirtualPathString, false);
        }

        public static string MakeVirtualPathAppRelative(string virtualPath, string applicationPath, bool nullIfNotInApp)
        {
            if (virtualPath == null)
            {
                throw new ArgumentNullException("virtualPath");
            }
            int length = applicationPath.Length;
            int num2 = virtualPath.Length;
            if ((num2 == (length - 1)) && StringUtil.StringStartsWithIgnoreCase(applicationPath, virtualPath))
            {
                return "~/";
            }
            if (!VirtualPathStartsWithVirtualPath(virtualPath, applicationPath))
            {
                if (nullIfNotInApp)
                {
                    return null;
                }
                return virtualPath;
            }
            if (num2 == length)
            {
                return "~/";
            }
            if (length == 1)
            {
                return ('~' + virtualPath);
            }
            return ('~' + virtualPath.Substring(length - 1));
        }

        public static string MakeVirtualPathAppRelativeOrNull(string virtualPath)
        {
            return MakeVirtualPathAppRelative(virtualPath, VirtualPath.AppDomainAppVirtualPathString, true);
        }

        public static bool PathEndsWithExtraSlash(string path)
        {
            if (path == null)
            {
                return false;
            }
            int length = path.Length;
            if ((length == 0) || (path[length - 1] != '\\'))
            {
                return false;
            }
            if ((length == 3) && (path[1] == ':'))
            {
                return false;
            }
            return true;
        }

        public static bool PathIsDriveRoot(string path)
        {
            return (((path != null) && (path.Length == 3)) && ((path[1] == ':') && (path[2] == '\\')));
        }

        public static string Reduce(string path)
        {
            string str = null;
            if (path != null)
            {
                int index = path.IndexOf('?');
                if (index >= 0)
                {
                    str = path.Substring(index);
                    path = path.Substring(0, index);
                }
            }
            path = FixVirtualPathSlashes(path);
            path = ReduceVirtualPath(path);
            if (str == null)
            {
                return path;
            }
            return (path + str);
        }

        public static string ReduceVirtualPath(string path)
        {
            int length = path.Length;
            int startIndex = 0;
            while (true)
            {
                startIndex = path.IndexOf('.', startIndex);
                if (startIndex < 0)
                {
                    return path;
                }
                if (((startIndex == 0) || (path[startIndex - 1] == '/')) && ((((startIndex + 1) == length) || (path[startIndex + 1] == '/')) || ((path[startIndex + 1] == '.') && (((startIndex + 2) == length) || (path[startIndex + 2] == '/')))))
                {
                    break;
                }
                startIndex++;
            }
            ArrayList list = new ArrayList();
            StringBuilder builder = new StringBuilder();
            startIndex = 0;
            do
            {
                int num3 = startIndex;
                startIndex = path.IndexOf('/', num3 + 1);
                if (startIndex < 0)
                {
                    startIndex = length;
                }
                if ((((startIndex - num3) <= 3) && ((startIndex < 1) || (path[startIndex - 1] == '.'))) && (((num3 + 1) >= length) || (path[num3 + 1] == '.')))
                {
                    if ((startIndex - num3) == 3)
                    {
                        if (list.Count == 0)
                        {
                            throw new HttpException(string.Format("Cannot_exit_up_top_directory"));
                        }
                        if ((list.Count == 1) && IsAppRelativePath(path))
                        {
                            return ReduceVirtualPath(MakeVirtualPathAppAbsolute(path));
                        }
                        builder.Length = (int)list[list.Count - 1];
                        list.RemoveRange(list.Count - 1, 1);
                    }
                }
                else
                {
                    list.Add(builder.Length);
                    builder.Append(path, num3, startIndex - num3);
                }
            }
            while (startIndex != length);
            string str = builder.ToString();
            if (str.Length != 0)
            {
                return str;
            }
            if ((length > 0) && (path[0] == '/'))
            {
                return "/";
            }
            return ".";
        }

        public static string RemoveSlashFromPathIfNeeded(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return null;
            }
            int length = path.Length;
            if ((length > 1) && (path[length - 1] == '/'))
            {
                return path.Substring(0, length - 1);
            }
            return path;
        }

        public static string SimpleCombine(string basepath, string relative)
        {
            if (HasTrailingSlash(basepath))
            {
                return (basepath + relative);
            }
            return (basepath + "/" + relative);
        }

        public static bool VirtualPathStartsWithAppPath(string virtualPath)
        {
            return VirtualPathStartsWithVirtualPath(virtualPath, VirtualPath.AppDomainAppVirtualPathString);
        }

        private static bool VirtualPathStartsWithVirtualPath(string virtualPath1, string virtualPath2)
        {
            if (virtualPath1 == null)
            {
                throw new ArgumentNullException("virtualPath1");
            }
            if (virtualPath2 == null)
            {
                throw new ArgumentNullException("virtualPath2");
            }
            if (!StringUtil.StringStartsWithIgnoreCase(virtualPath1, virtualPath2))
            {
                return false;
            }
            int length = virtualPath2.Length;
            if (virtualPath1.Length != length)
            {
                if (length == 1)
                {
                    return true;
                }
                if (virtualPath2[length - 1] == '/')
                {
                    return true;
                }
                if (virtualPath1[length] != '/')
                {
                    return false;
                }
            }
            return true;
        }
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Sike Mullivan
Software Developer (Senior)
United States United States
I am a Sr SharePoint Developer in Saint Louis, MO. I've been developing professionally for about four years now. For three years I worked for a Document Imaging company that developed applications for Scanning, Indexing, Migrating, and Searching SharePoint (MOSS, WSS). I'm now working on a team for a Cable company that customizes their internal and external SharePoint implementations.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 10 Jun 2009
Article Copyright 2009 by Sike Mullivan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid