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;
44 public bool IsAbsolute {
49 public bool IsRooted {
54 public bool IsAppRelative {
59 public string Original {
64 public string Absolute {
69 if (_absolute == null) {
70 string original = Original;
72 if (!VirtualPathUtility.IsRooted (original))
73 _absolute = MakeRooted (original);
77 if (VirtualPathUtility.IsAppRelative (_absolute))
78 _absolute = VirtualPathUtility.ToAbsolute (_absolute);
85 public string AppRelative {
90 if (_appRelative == null) {
91 string original = Original;
93 if (!VirtualPathUtility.IsRooted (original))
94 _appRelative = MakeRooted (original);
96 _appRelative = original;
98 if (VirtualPathUtility.IsAbsolute (_appRelative))
99 _appRelative = VirtualPathUtility.ToAppRelative (_appRelative);
106 public string AppRelativeNotRooted {
108 if (_appRelativeNotRooted == null)
109 _appRelativeNotRooted = AppRelative.Substring (2);
111 return _appRelativeNotRooted;
115 public string Extension {
117 if (_extension == null)
118 _extension = VirtualPathUtility.GetExtension (Original);
124 public string Directory {
126 if (_directory == null)
127 _directory = VirtualPathUtility.GetDirectory (Absolute);
133 public string CurrentRequestDirectory {
135 if (_currentRequestDirectory != null)
136 return _currentRequestDirectory;
138 HttpContext ctx = HttpContext.Current;
139 HttpRequest req = ctx != null ? ctx.Request : null;
141 return VirtualPathUtility.GetDirectory (req.CurrentExecutionFilePath);
146 set { _currentRequestDirectory = value; }
149 public VirtualPath (string vpath)
153 IsRooted = VirtualPathUtility.IsRooted (vpath);
154 IsAbsolute = VirtualPathUtility.IsAbsolute (vpath);
155 IsAppRelative = VirtualPathUtility.IsAppRelative (vpath);
158 public VirtualPath (string vpath, string baseVirtualDir)
161 CurrentRequestDirectory = baseVirtualDir;
164 public bool StartsWith (string s)
166 return StrUtils.StartsWith (Original, s);
169 // Assumes 'original' is NOT rooted
170 string MakeRooted (string original)
172 string reqdir = CurrentRequestDirectory;
174 if (!String.IsNullOrEmpty (reqdir))
175 return VirtualPathUtility.Combine (reqdir, original);
177 return VirtualPathUtility.Combine (HttpRuntime.AppDomainAppVirtualPath, original);
180 public void Dispose ()
184 _appRelativeNotRooted = null;
189 public override string ToString ()
194 public static VirtualPath PhysicalToVirtual (string physical_path)
196 if (String.IsNullOrEmpty (physical_path))
199 string appPhysicalPath = HttpRuntime.AppDomainAppPath;
200 if (!StrUtils.StartsWith (physical_path, appPhysicalPath))
203 string vp = physical_path.Substring (appPhysicalPath.Length - 1);
207 return new VirtualPath (vp);