2 // System.Web.VirtualPathUtility.cs
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Web.Util;
36 namespace System.Web {
38 public static class VirtualPathUtility
40 public static string AppendTrailingSlash (string virtualPath)
42 if (virtualPath == null)
45 int length = virtualPath.Length;
46 if (length == 0 || virtualPath [length - 1] == '/')
49 return virtualPath + "/";
52 public static string Combine (string basePath, string relativePath)
54 if (basePath == null || basePath == "")
55 throw new ArgumentNullException ("basePath");
57 if (relativePath == null || relativePath == "")
58 throw new ArgumentNullException ("relativePath");
60 if (basePath [0] != '/')
61 throw new ArgumentException ("basePath is not an absolute path", "basePath");
63 if (relativePath [0] != '/')
64 return "/" + relativePath;
66 return UrlUtils.Combine (basePath, relativePath);
69 public static string GetDirectory (string virtualPath)
71 if (virtualPath == null || virtualPath == "") // Yes, "" throws an ArgumentNullException
72 throw new ArgumentNullException ("virtualPath");
74 if (virtualPath [0] != '/')
75 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
77 string result = UrlUtils.GetDirectory (virtualPath);
78 return AppendTrailingSlash (result);
81 public static string GetExtension (string virtualPath)
83 string filename = GetFileName (virtualPath);
84 int dot = filename.LastIndexOf ('.');
85 if (dot == -1 || dot == filename.Length + 1)
88 return filename.Substring (dot);
91 public static string GetFileName (string virtualPath)
93 if (virtualPath == null || virtualPath == "") // Yes, "" throws an ArgumentNullException
94 throw new ArgumentNullException ("virtualPath");
96 return UrlUtils.GetFile (RemoveTrailingSlash (virtualPath));
99 public static bool IsAbsolute (string virtualPath)
101 if (virtualPath == "" || virtualPath == null)
102 throw new ArgumentNullException ("virtualPath");
104 return (virtualPath [0] == '/');
107 public static bool IsAppRelative (string virtualPath)
109 if (virtualPath == null || virtualPath == "")
110 throw new ArgumentNullException ("virtualPath");
112 string vpath = HttpRuntime.AppDomainAppVirtualPath;
116 return virtualPath.StartsWith (AppendTrailingSlash (vpath));
119 public static string MakeRelative (string fromPath, string toPath)
121 if (fromPath == null || toPath == null)
122 throw new NullReferenceException (); // yeah!
127 if (toPath [0] != '/')
128 throw new ArgumentOutOfRangeException (); // This is what MS does.
130 if (fromPath.Length > 0 && fromPath [0] != '/')
131 throw new ArgumentOutOfRangeException (); // This is what MS does.
133 Uri from = new Uri ("http://nothing" + fromPath);
134 return from.MakeRelativeUri (new Uri ("http://nothing" + toPath)).AbsolutePath;
137 public static string RemoveTrailingSlash (string virtualPath)
139 if (virtualPath == null || virtualPath == "")
142 int last = virtualPath.Length - 1;
143 if (last == 0 || virtualPath [last] != '/')
146 return virtualPath.Substring (0, last);
149 public static string ToAbsolute (string virtualPath)
151 string apppath = HttpRuntime.AppDomainAppVirtualPath;
153 throw new HttpException ("The path to the application is not known");
155 return ToAbsolute (virtualPath,apppath);
158 public static string ToAbsolute (string virtualPath, string applicationPath)
160 if (applicationPath == null || applicationPath == "")
161 throw new ArgumentNullException ("applicationPath");
163 if (virtualPath == null || virtualPath == "")
164 throw new ArgumentNullException ("virtualPath");
166 if (virtualPath.StartsWith (".."))
167 throw new ArgumentException (String.Format ("Relative path not allowed: '{0}'", virtualPath));
169 if (applicationPath [0] != '/')
170 throw new ArgumentOutOfRangeException ("appPath is not rooted", "applicationPath");
172 return UrlUtils.Combine (applicationPath, virtualPath);
176 public static string ToAppRelative (string virtualPath)
178 string apppath = HttpRuntime.AppDomainAppVirtualPath;
180 throw new HttpException ("The path to the application is not known");
182 return ToAppRelative (apppath, virtualPath);
185 public static string ToAppRelative (string virtualPath, string applicationPath)
187 if (applicationPath == null || applicationPath == "")
188 throw new ArgumentNullException ("applicationPath");
190 if (virtualPath == null || virtualPath == "")
191 throw new ArgumentNullException ("virtualPath");
193 if (virtualPath.StartsWith (".."))
194 throw new ArgumentException (String.Format ("Relative path not allowed: '{0}'", virtualPath));
196 if (applicationPath [0] != '/')
197 throw new ArgumentOutOfRangeException ("appPath is not rooted", "applicationPath");
199 return MakeRelative (applicationPath, virtualPath);