Search code examples
asp.netasp.net-mvcdynamic-data

How can I mix DynamicData Routes with Mvc Routes?


I am trying to create an Area for running DynamicData with scaffolding as an administration part. It's probably working as it should but I can't get the routing to work.

In my global.asax I have the following function to register the routes:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("public/{*pathInfo}");
            routes.IgnoreRoute("{resource}.js"); 
            routes.IgnoreRoute("{resource}.css"); 

            routes.MapRoute("OrderComment", "Order/{id}/comments",
                new { controller = "Order", action = "Comments", id = 0 }, // Parameter defaults
                new { controller = new NotEqual("Register", "Product", "Admin") });

            routes.MapRoute("Account", "Account/Edit/{accountId}",
                            new { controller = "Account", action = "Edit", accountId = 0 }, // Parameter defaults
                            new { controller = new NotEqual("Register", "Product", "Admin") });


            routes.MapRoute("Default", // Route name
                            "{controller}/{action}/{id}", // URL with parameters
                            new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                            new { controller = new NotEqual("Register", "Product", "Admin") });


            if (HostingEnvironment.IsHosted) {
                routes.Add(new ServiceRoute("Register", new WebServiceHostFactory(), typeof(RegisterService)));
                routes.Add(new ServiceRoute("Product", new WebServiceHostFactory(), typeof(ProductService)));
            }
        }

In my area I put all the DynamicData specifics like this:

public class AdminAreaRegistration : AreaRegistration {

private static MetaModel s_defaultModel = new MetaModel();
public static MetaModel DefaultModel
{
    get
    {
        return s_defaultModel;
    }
}

public override string AreaName
{
    get
    {
        return "Admin";
    }
}

public override void RegisterArea(AreaRegistrationContext context)
{
    DefaultModel.RegisterContext(typeof(diglifeEntities), new ContextConfiguration { ScaffoldAllTables = true });
    context.Routes.RouteExistingFiles = true;
    context.Routes.Add(new DynamicDataRoute("Admin/{table}/{action}.aspx")
    {
        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
        Model = DefaultModel,
    });
}

}

Unfortunately the "best" thing that happened to me so far was that when I check the default tables I get a message about (MetaModel.VisibleTables)

Message = "Value cannot be null.\r\nParameter name: principal"

with the following stacktrace:

   at System.Web.DynamicData.ModelProviders.TableProvider.CanRead(IPrincipal principal)
   at System.Web.DynamicData.MetaTable.CanRead(IPrincipal principal)
   at System.Web.DynamicData.MetaModel.IsTableVisible(MetaTable table)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Web.DynamicData.MetaModel.get_VisibleTables()

The problem is that I don't know why this is. Is it the routing or something else? Is it perhaps some security thing or can I not put dynamic data inside an area?

The DynamicData folder itself is located in the root of the application since that is the convention it uses and I have no intention to override that.

Edit: I found this post but it doesn't seem to do anything for me.


Solution

  • The solution to my problems (yet again answered by myself) is to create a PageRoute inside the Admin area registration pointing to this:

    context.Routes.MapPageRoute("Admin", 
                                "admin/{pagename}", 
                                "~/Areas/Admin/Default.aspx", 
                                true);