private static void InitializeDynamicData(RouteCollection routes, string root, IAppConfiguration configuration) { try { DefaultModel.RegisterContext( new EFCodeFirstDataModelProvider( () => new EntitiesContext(configuration.SqlConnectionString, readOnly: false)), // DB Admins do not need to respect read-only mode. configuration: new ContextConfiguration { ScaffoldAllTables = true }); } catch (SqlException e) { QuietLog.LogHandledException(e); return; } catch (DataException e) { QuietLog.LogHandledException(e); return; } // This route must come first to prevent some other route from the site to take over _route = new DynamicDataRoute(root + "/{table}/{action}") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = DefaultModel }; routes.Insert(0, _route); routes.MapPageRoute( "dd_default", root, "~/Areas/Admin/DynamicData/Default.aspx"); }
private string GetPageVirtualPathNoCache(DynamicDataRoute route, MetaTable table, string action) { // The view name defaults to the action string viewName = route.ViewName ?? action; // First, get the path to the custom page string customPageVirtualPath = GetCustomPageVirtualPath(table, viewName); if (VirtualPathProvider.FileExists(customPageVirtualPath)) { return(customPageVirtualPath); } else { if (table.Scaffold) { // If it doesn't exist, try the scaffolded page, but only if scaffolding is enabled on this table return(GetScaffoldPageVirtualPath(table, viewName)); } else { // If scaffolding is disabled, null the path so BuildManager doesn't get called. return(null); } } }
private string GetPageVirtualPath(DynamicDataRoute route, MetaTable table, string action) { long cacheKey = Misc.CombineHashCodes(table, route.ViewName ?? action); Dictionary<long, string> virtualPathCache = GetVirtualPathCache(); string virtualPath; if (!virtualPathCache.TryGetValue(cacheKey, out virtualPath)) { virtualPath = GetPageVirtualPathNoCache(route, table, action); lock (virtualPathCache) { virtualPathCache[cacheKey] = virtualPath; } } return virtualPath; }
/// <summary> /// Create a handler to process a Dynamic Data request /// </summary> /// <param name="route">The Route that was matched</param> /// <param name="table">The MetaTable found in the route</param> /// <param name="action">The Action found in the route</param> /// <returns></returns> public virtual IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string action) { // First, get the path to the page (could be custom, shared, or null) string virtualPath = GetPageVirtualPath(route, table, action); if (virtualPath != null) { // Gets called only for custom pages that we know exist or templates that may or may not // exist. This method will throw if virtualPath does not exist, which is fine for templates // but is not fine for custom pages. return CreateHandlerCallback(virtualPath); } else { // This should only occur in the event that scaffolding is disabled and the custom page // virtual path does not exist. return null; } }
private string GetPageVirtualPath(DynamicDataRoute route, MetaTable table, string action) { long cacheKey = Misc.CombineHashCodes(table, route.ViewName ?? action); Dictionary <long, string> virtualPathCache = GetVirtualPathCache(); string virtualPath; if (!virtualPathCache.TryGetValue(cacheKey, out virtualPath)) { virtualPath = GetPageVirtualPathNoCache(route, table, action); lock (virtualPathCache) { virtualPathCache[cacheKey] = virtualPath; } } return(virtualPath); }
/// <summary> /// Create a handler to process a Dynamic Data request /// </summary> /// <param name="route">The Route that was matched</param> /// <param name="table">The MetaTable found in the route</param> /// <param name="action">The Action found in the route</param> /// <returns></returns> public virtual IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string action) { // First, get the path to the page (could be custom, shared, or null) string virtualPath = GetPageVirtualPath(route, table, action); if (virtualPath != null) { // Gets called only for custom pages that we know exist or templates that may or may not // exist. This method will throw if virtualPath does not exist, which is fine for templates // but is not fine for custom pages. return(CreateHandlerCallback(virtualPath)); } else { // This should only occur in the event that scaffolding is disabled and the custom page // virtual path does not exist. return(null); } }
static RouteContext MakeRouteContext(RequestContext context, DynamicDataRoute route, string action, MetaTable table) { RouteData rd = null; if (route == null) { rd = context.RouteData; route = rd.Route as DynamicDataRoute; } if (route != null) { if (action == null) { if (rd == null) { rd = context.RouteData; } action = route.GetActionFromRouteData(rd); } if (table == null) { if (rd == null) { rd = context.RouteData; } table = route.GetTableFromRouteData(rd); } } return(new RouteContext() { Route = route, Action = action, Table = table, Context = context }); }
internal static RequestContext GetRequestContext(HttpContextBase httpContext) { Debug.Assert(httpContext != null); // Look for the RequestContext in the HttpContext var requestContext = httpContext.Items[s_requestContextKey] as RequestContext; // If the current request didn't go through the routing engine (e.g. normal page), // there won't be a RequestContext. If so, create a new one and save it if (requestContext == null) { var routeData = new RouteData(); requestContext = new RequestContext(httpContext, routeData); // Add the query string params to the route data. This allows non routed pages to support filtering. DynamicDataRoute.AddQueryStringParamsToRouteData(httpContext, routeData); httpContext.Items[s_requestContextKey] = requestContext; } return(requestContext); }
public virtual IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string action) { // .NET bug emulation mode if (route == null || table == null || action == null) { throw new NullReferenceException(); } // NOTE: all code below is a result of guessing as no tests succeed for this // call so far! IHttpHandler ret = null; // Give custom pages a chance string viewName = String.IsNullOrEmpty(action) ? route.ViewName : action; string path = GetCustomPageVirtualPath(table, viewName); // Pages might be in app resources, need to use a VPP VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider; if (vpp != null && vpp.FileExists(path)) { ret = BuildManager.CreateInstanceFromVirtualPath(path, typeof(Page)) as IHttpHandler; } if (ret != null) { return(ret); } path = GetScaffoldPageVirtualPath(table, viewName); if (vpp != null && vpp.FileExists(path)) { ret = BuildManager.CreateInstanceFromVirtualPath(path, typeof(Page)) as IHttpHandler; } return(ret); }
/// <summary> /// Creates the handler. /// </summary> /// <param name="route">The route.</param> /// <param name="table">The table.</param> /// <param name="view">The action.</param> /// <returns>An IHttpHandler</returns> public override IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string view) { var httpContext = HttpContext.Current; if (httpContext != null) { string[] usersRoles = new string[] { "Guest" }; if (httpContext.User.Identity.Name != "") { usersRoles = Roles.GetRolesForUser(httpContext.User.Identity.Name); if (usersRoles.Length == 0) usersRoles = new string[] { "Guest" }; } var tablePermissions = table.Attributes.OfType<DataAccess>(); if (tablePermissions.Count() == 0) return null; foreach (var tp in tablePermissions) { if (tp.HasAnyRole(usersRoles)) { // if no action is allowed return no route var tpTableView = tp.TableViews.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); var tpRowView = tp.RowViews.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tpTableView.Contains(view)) return base.CreateHandler(route, table, view); if (tpRowView.Contains(view)) return base.CreateHandler(route, table, view); } } } return null; }
public void GetRouteData () { var r = new DynamicDataRoute ("{table}/{action}.aspx"); // We need one which overloads CreateHandler r.RouteHandler = new MyDynamicDataRouteHandler (); var wrapper = new MyHttpContextWrapper (); var request = wrapper.Request as MyHttpRequestWrapper; request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx"); request.SetProperty ("PathInfo", String.Empty); // This must be non-null because DynamicData doesn't care to check whether the returned // value is null or not... request.SetProperty ("QueryString", new NameValueCollection ()); // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return // null (even though the Route class will find a match) RouteData rd = r.GetRouteData (wrapper); Assert.IsNull (rd, "#A1"); request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/BazTable/List.aspx"); rd = r.GetRouteData (wrapper); Assert.IsNotNull (rd, "#B1"); }
public override IHttpHandler CreateHandler (DynamicDataRoute route, MetaTable table, string action) { return new Page () as IHttpHandler; }
static RouteContext MakeRouteContext (RequestContext context, DynamicDataRoute route, string action, MetaTable table) { RouteData rd = null; if (route == null) { rd = context.RouteData; route = rd.Route as DynamicDataRoute; } if (route != null) { if (action == null) { if (rd == null) rd = context.RouteData; action = route.GetActionFromRouteData (rd); } if (table == null) { if (rd == null) rd = context.RouteData; table = route.GetTableFromRouteData (rd); } } return new RouteContext () { Route = route, Action = action, Table = table, Context = context}; }
public void GetActionFromRouteData2 () { var r = new DynamicDataRoute ("x"); var rd = new RouteData (); rd.Values["Action"] = "y"; var a = r.GetActionFromRouteData (rd); Assert.AreEqual ("y", a); }
public void GetActionFromRouteData () { var r = new DynamicDataRoute ("x2"); var rd = new RouteData (); // rd must have "Action" value r.GetActionFromRouteData (rd); }
public void BaseDefaultsModification_10 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Defaults = new RouteValueDictionary () { {"Table", "FooWithDefaultsTable"} }, Table = "BazTable", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNotNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#B1"); Assert.AreEqual (1, ddr.Defaults.Count, "#B1-1"); Assert.AreEqual ("BazTable", ddr.Defaults["Table"], "#B1-2"); ddr.Table = "AnotherTable"; ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#C1"); Assert.AreEqual (1, ddr.Defaults.Count, "#C1-1"); Assert.AreEqual ("BazTable", ddr.Defaults["Table"], "#C1-2"); }
public void Constructor () { // other tests create MetaModel and set Default and this test does not always run first, so it does not make sense anymore. //Assert.IsNull (MetaModel.Default, "#1"); bool isFirst = (MetaModel.Default == null); var m = new MetaModel (); // it automatically fills Default if (isFirst) Assert.AreEqual (m, MetaModel.Default, "#2"); var r = new DynamicDataRoute ("Dynamic1"); Assert.AreEqual (MetaModel.Default, r.Model, "#1"); Assert.IsNull (r.Action, "#2"); Assert.IsNull (r.Table, "#3"); Assert.IsNull (r.ViewName, "#4"); Assert.IsNotNull (r.RouteHandler, "#5"); Assert.IsNotNull (r.Model, "#6"); Assert.IsNull (r.RouteHandler.Model, "#7"); // irrelevant to route's MetaModel }
public void BaseDefaultsModification_8 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Table = String.Empty, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); AssertExtensions.Throws<ArgumentException> (() => { ddr.GetVirtualPath (new RequestContext (hc, rd), null); }, "#A1"); }
public void BaseDefaultsModification_9 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Defaults = new RouteValueDictionary () { {"Action", "InitialAction"} }, Action = PageAction.Details, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNotNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#B1"); Assert.AreEqual (1, ddr.Defaults.Count, "#B1-1"); Assert.AreEqual (PageAction.Details, ddr.Defaults["Action"], "#B1-2"); ddr.Action = "MyAction"; ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#C1"); Assert.AreEqual (1, ddr.Defaults.Count, "#C1-1"); Assert.AreEqual (PageAction.Details, ddr.Defaults["Action"], "#B1-2"); }
public void BaseDefaultsModification_7 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Table = null, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNull (ddr.Defaults, "#B1"); }
public virtual IHttpHandler CreateHandler (DynamicDataRoute route, MetaTable table, string action) { // .NET bug emulation mode if (route == null || table == null || action == null) throw new NullReferenceException (); // NOTE: all code below is a result of guessing as no tests succeed for this // call so far! IHttpHandler ret = null; // Give custom pages a chance string viewName = String.IsNullOrEmpty (action) ? route.ViewName : action; string path = GetCustomPageVirtualPath (table, viewName); // Pages might be in app resources, need to use a VPP VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider; if (vpp != null && vpp.FileExists (path)) ret = BuildManager.CreateInstanceFromVirtualPath (path, typeof (Page)) as IHttpHandler; if (ret != null) return ret; path = GetScaffoldPageVirtualPath (table, viewName); if (vpp != null && vpp.FileExists (path)) ret = BuildManager.CreateInstanceFromVirtualPath (path, typeof (Page)) as IHttpHandler; return ret; }
public void GetTableFromRouteData () { var r = new DynamicDataRoute ("x"); var rd = new RouteData (); // rd must have "Table" value r.GetTableFromRouteData (rd); }
public void GetTableFromRouteData3 () { var r = new DynamicDataRoute ("x"); var rd = new RouteData (); rd.Values["Table"] = "FooTable"; var t = r.GetTableFromRouteData (rd); }
public void GetTableFromRouteData2 () { var r = new DynamicDataRoute ("x"); r.Model = new MetaModel (); var rd = new RouteData (); rd.Values["Table"] = "y"; r.GetTableFromRouteData (rd); // no such table }
private string GetPageVirtualPathNoCache(DynamicDataRoute route, MetaTable table, string action) { // The view name defaults to the action string viewName = route.ViewName ?? action; // First, get the path to the custom page string customPageVirtualPath = GetCustomPageVirtualPath(table, viewName); if (VirtualPathProvider.FileExists(customPageVirtualPath)) { return customPageVirtualPath; } else { if (table.Scaffold) { // If it doesn't exist, try the scaffolded page, but only if scaffolding is enabled on this table return GetScaffoldPageVirtualPath(table, viewName); } else { // If scaffolding is disabled, null the path so BuildManager doesn't get called. return null; } } }
public void RouteHandler () { var r = new DynamicDataRoute ("{table}/{action}.aspx"); Assert.IsNotNull (r.RouteHandler, "#A1"); Assert.AreEqual (typeof (DynamicDataRouteHandler), r.RouteHandler.GetType (), "#A1-1"); r.RouteHandler = null; Assert.IsNull (r.RouteHandler, "#A2"); }