views:

735

answers:

3

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.

A: 

For ASP.NET MVC to work, you will have to match the URL you are trying to access with the list of routes.

For your current global.asax, example of valid URLs are:

http://domain/AnyController/AnyAction/AnyParameter
http://domain/Assignment/
http://domain/Assignment/AnyAction/AnyParameter

MVC requests are redirected to the proper Controller class, Action method, with parameters as passed in. MVC request is not redirected to any ASPX class. This is the difference between ASP.NET MVC and vanilla ASP.NET Page.

Adrian Godong
+2  A: 

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.

Robert Harvey
added route, no luck
RSolberg
It should be matching the new route. DD matches the first piece in the URL, Work_Phases maps to the name of the method in your controller, NameOfController should be the name of your controller (omit the word Controller in your route).
Robert Harvey
There is no controller, I think that is the issue at hand.
RSolberg
@RSolberg, see my edit.
Robert Harvey
+3  A: 

The url doesn't match your dynamic data route because it doesn't fit the constraints you put on it. You're requesting action ListDetails but only these actions are allowed

Constraints = new RouteValueDictionary(
                    new { action = "List|Details|Edit|Insert" }

EDIT: are you sure that an action called ListDetails exists? Then modify the constraints above to

Constraints = new RouteValueDictionary(
                    new { action = "ListDetails|List|Details|Edit|Insert" }

Just to be sure that it's the constraints that's causing the route to be ignored, can you try one of the default actions? E.g.

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

chris166