2 // System.Web.VirtualPath.cs
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2008-2009 Novell, Inc
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Compilation;
33 using System.Web.Util;
37 internal class VirtualPath : IDisposable
41 string _appRelativeNotRooted;
44 string _directoryNoNormalize;
45 string _currentRequestDirectory;
48 public bool IsAbsolute {
58 public bool IsRooted {
63 public bool IsAppRelative {
68 public string Original {
73 public string Absolute {
78 if (_absolute == null) {
79 string original = Original;
81 if (!VirtualPathUtility.IsRooted (original))
82 _absolute = MakeRooted (original);
86 if (VirtualPathUtility.IsAppRelative (_absolute))
87 _absolute = VirtualPathUtility.ToAbsolute (_absolute);
94 public string AppRelative {
99 if (_appRelative == null) {
100 string original = Original;
102 if (!VirtualPathUtility.IsRooted (original))
103 _appRelative = MakeRooted (original);
105 _appRelative = original;
107 if (VirtualPathUtility.IsAbsolute (_appRelative))
108 _appRelative = VirtualPathUtility.ToAppRelative (_appRelative);
115 public string AppRelativeNotRooted {
117 if (_appRelativeNotRooted == null)
118 _appRelativeNotRooted = AppRelative.Substring (2);
120 return _appRelativeNotRooted;
124 public string Extension {
126 if (_extension == null)
127 _extension = VirtualPathUtility.GetExtension (Original);
133 public string Directory {
135 if (_directory == null)
136 _directory = VirtualPathUtility.GetDirectory (Absolute);
142 public string DirectoryNoNormalize {
144 if (_directoryNoNormalize == null)
145 _directoryNoNormalize = VirtualPathUtility.GetDirectory (Absolute, false);
147 return _directoryNoNormalize;
151 public string CurrentRequestDirectory {
153 if (_currentRequestDirectory != null)
154 return _currentRequestDirectory;
156 HttpContext ctx = HttpContext.Current;
157 HttpRequest req = ctx != null ? ctx.Request : null;
159 return VirtualPathUtility.GetDirectory (req.CurrentExecutionFilePath);
164 set { _currentRequestDirectory = value; }
167 public string PhysicalPath {
169 if (_physicalPath != null)
170 return _physicalPath;
172 HttpContext ctx = HttpContext.Current;
173 HttpRequest req = ctx != null ? ctx.Request : null;
175 _physicalPath = req.MapPath (Absolute);
179 return _physicalPath;
183 public VirtualPath (string vpath)
184 : this (vpath, null, false)
188 public VirtualPath (string vpath, string baseVirtualDir)
189 : this (vpath, null, false)
191 CurrentRequestDirectory = baseVirtualDir;
194 public VirtualPath (string vpath, string physicalPath, bool isFake)
196 IsRooted = VirtualPathUtility.IsRooted (vpath);
197 IsAbsolute = VirtualPathUtility.IsAbsolute (vpath);
198 IsAppRelative = VirtualPathUtility.IsAppRelative (vpath);
201 if (String.IsNullOrEmpty (physicalPath))
202 throw new ArgumentException ("physicalPath");
204 _physicalPath = physicalPath;
205 Original = "~/" + Path.GetFileName (_physicalPath);
213 public bool StartsWith (string s)
215 return StrUtils.StartsWith (Original, s);
218 // Assumes 'original' is NOT rooted
219 string MakeRooted (string original)
221 string reqdir = CurrentRequestDirectory;
223 if (!String.IsNullOrEmpty (reqdir))
224 return VirtualPathUtility.Combine (reqdir, original);
226 return VirtualPathUtility.Combine (HttpRuntime.AppDomainAppVirtualPath, original);
229 public void Dispose ()
233 _appRelativeNotRooted = null;
238 public override string ToString ()
240 string ret = Original;
242 if (String.IsNullOrEmpty (ret))
243 return GetType ().ToString ();
246 ret += " [fake: " + PhysicalPath + "]";
251 public static VirtualPath PhysicalToVirtual (string physical_path)
253 if (String.IsNullOrEmpty (physical_path))
256 string appPhysicalPath = HttpRuntime.AppDomainAppPath;
257 if (!StrUtils.StartsWith (physical_path, appPhysicalPath))
260 string vp = physical_path.Substring (appPhysicalPath.Length - 1);
264 return new VirtualPath (vp);