2 // System.Web.VirtualPath.cs
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2008 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.
31 using System.Web.Util;
35 internal class VirtualPath : IDisposable
39 string _appRelativeNotRooted;
42 string _currentRequestDirectory;
45 public bool IsAbsolute {
50 public bool IsRooted {
55 public bool IsAppRelative {
60 public string Original {
65 public string Absolute {
70 if (_absolute == null) {
71 string original = Original;
73 if (!VirtualPathUtility.IsRooted (original))
74 _absolute = MakeRooted (original);
78 if (VirtualPathUtility.IsAppRelative (_absolute))
79 _absolute = VirtualPathUtility.ToAbsolute (_absolute);
86 public string AppRelative {
91 if (_appRelative == null) {
92 string original = Original;
94 if (!VirtualPathUtility.IsRooted (original))
95 _appRelative = MakeRooted (original);
97 _appRelative = original;
99 if (VirtualPathUtility.IsAbsolute (_appRelative))
100 _appRelative = VirtualPathUtility.ToAppRelative (_appRelative);
107 public string AppRelativeNotRooted {
109 if (_appRelativeNotRooted == null)
110 _appRelativeNotRooted = AppRelative.Substring (2);
112 return _appRelativeNotRooted;
116 public string Extension {
118 if (_extension == null)
119 _extension = VirtualPathUtility.GetExtension (Original);
125 public string Directory {
127 if (_directory == null)
128 _directory = VirtualPathUtility.GetDirectory (Absolute);
134 public string CurrentRequestDirectory {
136 if (_currentRequestDirectory != null)
137 return _currentRequestDirectory;
139 HttpContext ctx = HttpContext.Current;
140 HttpRequest req = ctx != null ? ctx.Request : null;
142 return VirtualPathUtility.GetDirectory (req.CurrentExecutionFilePath);
147 set { _currentRequestDirectory = value; }
150 public string PhysicalPath {
152 if (_physicalPath != null)
153 return _physicalPath;
155 HttpContext ctx = HttpContext.Current;
156 HttpRequest req = ctx != null ? ctx.Request : null;
158 _physicalPath = req.MapPath (Absolute);
162 return _physicalPath;
166 public VirtualPath (string vpath)
170 IsRooted = VirtualPathUtility.IsRooted (vpath);
171 IsAbsolute = VirtualPathUtility.IsAbsolute (vpath);
172 IsAppRelative = VirtualPathUtility.IsAppRelative (vpath);
175 public VirtualPath (string vpath, string baseVirtualDir)
178 CurrentRequestDirectory = baseVirtualDir;
181 public bool StartsWith (string s)
183 return StrUtils.StartsWith (Original, s);
186 // Assumes 'original' is NOT rooted
187 string MakeRooted (string original)
189 string reqdir = CurrentRequestDirectory;
191 if (!String.IsNullOrEmpty (reqdir))
192 return VirtualPathUtility.Combine (reqdir, original);
194 return VirtualPathUtility.Combine (HttpRuntime.AppDomainAppVirtualPath, original);
197 public void Dispose ()
201 _appRelativeNotRooted = null;
206 public override string ToString ()
211 public static VirtualPath PhysicalToVirtual (string physical_path)
213 if (String.IsNullOrEmpty (physical_path))
216 string appPhysicalPath = HttpRuntime.AppDomainAppPath;
217 if (!StrUtils.StartsWith (physical_path, appPhysicalPath))
220 string vp = physical_path.Substring (appPhysicalPath.Length - 1);
224 return new VirtualPath (vp);