5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Marek Habersack <mhabersack@novell.com>
8 // Copyright (C) 2008-2009 Novell Inc. http://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.
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.Security.Permissions;
37 using System.Security.Principal;
38 using System.Web.Caching;
39 using System.Web.Routing;
41 namespace System.Web.DynamicData
43 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public class DynamicDataRoute : Route
47 static readonly object initLock = new object ();
50 public DynamicDataRoute (string url)
53 Model = MetaModel.Default;
54 RouteHandler = new DynamicDataRouteHandler ();
57 public string Action { get; set; }
59 public MetaModel Model { get; set; }
61 public DynamicDataRouteHandler RouteHandler {
62 get { return base.RouteHandler as DynamicDataRouteHandler; }
63 set { base.RouteHandler = value; }
66 public string Table { get; set; }
68 public string ViewName { get; set; }
70 void EnsureInitialized ()
75 // We need to lock since we might be stored in the RouteTable.Routes
76 // collection which might be accessed from many concurrent requests.
83 DynamicDataRouteHandler rh = RouteHandler;
87 string action = Action, table = Table;
88 if (action == null && table == null)
91 RouteValueDictionary defaults = Defaults;
93 Defaults = defaults = new RouteValueDictionary ();
96 // Force check for table existence
97 MetaModel model = Model ?? MetaModel.Default;
99 Model.GetTable (table);
101 if (defaults.ContainsKey ("Table"))
102 defaults ["Table"] = table;
104 defaults.Add ("Table", table);
107 if (action != null) {
108 if (defaults.ContainsKey ("Action"))
109 defaults ["Action"] = action;
111 defaults.Add ("Action", action);
116 public string GetActionFromRouteData (RouteData routeData)
118 if (routeData == null)
119 throw new ArgumentNullException ("routeData");
120 return routeData.GetRequiredString ("Action");
123 public override RouteData GetRouteData (HttpContextBase httpContext)
125 EnsureInitialized ();
126 RouteData rd = base.GetRouteData (httpContext);
131 MetaModel model = Model ?? MetaModel.Default;
133 if (model == null || !model.TryGetTable (rd.GetRequiredString ("Table"), out table))
139 public MetaTable GetTableFromRouteData (RouteData routeData)
141 if (routeData == null)
142 throw new ArgumentNullException ("routeData");
143 var t = routeData.GetRequiredString ("Table");
145 throw new InvalidOperationException ("MetaModel must be set to the DynamicDataRoute before retrieving MetaTable");
147 return Model.GetTable (t);
150 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
152 EnsureInitialized ();
153 return base.GetVirtualPath (requestContext, values);