R – ASP.NET MVC App Routing Not Working For Dynamic Data WebForm Pages

asp.net-mvcrouting

I need the correct Global.asax settings in order for my Dynamic Data site to run under an ASP.NET MVC project. The routing currently appears to be my issue.

Here is my global.asax:

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

    MetaModel model = new MetaModel();
    model.RegisterContext(typeof(Models.DBDataContext), new ContextConfiguration() { ScaffoldAllTables = true });
    routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
        Model = model
    });

    routes.MapRoute(
        "Assignment",
        "Assignment/{action}/{page}",
        new { controller = "Assignment", action = "Index", page = "" });

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

}

Link that I'm trying to use is:

http://localhost:64205/DD/Work_Phases/ListDetails.aspx

I am getting the following message:

Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.

Requested URL:
/DD/Work_Phases/ListDetails.aspx

I've tried replacing DD with DynamicData since the folder inside of the app is DynamicData and that yielded the exact same result.

Best Answer

The URL

http://localhost:64205/DD/Work_Phases/ListDetails.aspx

is matching your second (default) route, which is trying to hit a controller called "DD".

You may need another route entry that looks something like this:

routes.MapRoute(
    "DD",
    "DD/{action}/{page}",
    new { controller = "NameOfController", action = "Index", page = "" }
);

...although I can't imagine why you would need to pass a page parameter. The page view that is hit depends on the return action of the controller method.


For a better look at integrating Dynamic Data with ASP.NET MVC, have a look at Scott Hanselman's Plugin-Hybrids article. He has some details about handling the .ASPX files that are not part of MVC. In particular, if you have an .ASPX that you don't want to be processed by the ASP.NET MVC controllers, you can install an Ignore Route:

routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");

It should be noted that ASP.NET MVC is configured out of the box to ignore URL requests for files that physically exist on the disk, although Scott's IgnoreRoute technique is apparently more efficient.

Related Topic