Merge pull request #2679 from lambdageek/dev/monoerror-reflection
[mono.git] / mcs / class / System.Web / System.Web.Routing / PageRouteHandler.cs
1 //
2 // System.Web.Compilation.BuildManager
3 //
4 // Authors:
5 //      Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2009-2010 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.ComponentModel;
35 using System.Text;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.Compilation;
39
40 namespace System.Web.Routing
41 {
42         public class PageRouteHandler : IRouteHandler
43         {
44                 public bool CheckPhysicalUrlAccess { get; private set; }
45                 
46                 public string VirtualPath { get; private set; }
47                 
48                 public PageRouteHandler (string virtualPath)
49                         : this (virtualPath, true)
50                 {
51                 }
52
53                 public PageRouteHandler (string virtualPath, bool checkPhysicalUrlAccess)
54                 {
55                         if (String.IsNullOrEmpty (virtualPath) || !virtualPath.StartsWith ("~/"))
56                                 throw new ArgumentException ("VirtualPath must be a non empty string starting with ~/", "virtualPath");
57                         
58                         VirtualPath = virtualPath;
59                         CheckPhysicalUrlAccess = checkPhysicalUrlAccess;
60                 }
61
62                 [MonoTODO ("Implement checking physical URL access")]
63                 public virtual IHttpHandler GetHttpHandler (RequestContext requestContext)
64                 {
65                         if (requestContext == null)
66                                 throw new ArgumentNullException ("requestContext");
67
68                         string vpath = GetSubstitutedVirtualPath (requestContext);
69                         int idx = vpath.IndexOf ('?');
70                         if (idx > -1)
71                                 vpath = vpath.Substring (0, idx);
72
73                         if (String.IsNullOrEmpty (vpath))
74                                 return null;
75                         
76                         return BuildManager.CreateInstanceFromVirtualPath (vpath, typeof (Page)) as IHttpHandler;
77                 }
78
79                 public string GetSubstitutedVirtualPath (RequestContext requestContext)
80                 {
81                         if (requestContext == null)
82                                 throw new ArgumentNullException ("requestContext");
83
84                         RouteData rd = requestContext.RouteData;
85                         Route route = rd != null ? rd.Route as Route: null;
86                         if (route == null)
87                                 return VirtualPath;
88                         
89                         VirtualPathData vpd = new Route (VirtualPath.Substring (2), this).GetVirtualPath (requestContext, rd.Values);
90                         if (vpd == null)
91                                 return VirtualPath;
92
93                         return "~/" + vpd.VirtualPath;
94                 }
95         }
96 }