1

I am working on an ASP.NET mvc 3 site that contains several project entities, and then each project has several associated subpages, each that works with a component of the project.

So for instance, I could have a project with several photos, milestones, user info, etc. I have a Project Index view, as well as a Project Home which links to several component pages. Most of the components have two views, Index, and Edit/View.

So I set up a route for the edits and views. Note that my route is in an AREA called ProjectManagement

   context.MapRoute(
            "ProjectManagement_ProjectPageSingle",
            "ProjectManagement/{controller}/{action}/{projectNumber}/{projectChildId}",
            new { controller = "Project", action = "Home" }
   );

and my controller actions all look similar to this:

 public ActionResult Edit(string projectNumber, string projectChildId)

This works well and good when I type in the URL directly in the browser. For instance:

~/ProjectManagement/Milestone/Edit/39999P110175/1   

however, when I generate an action link using:

 <a href="@Url.Action("Edit", new { projectNumber = Model.Project.ProjectNumber, projectChildId = entry.Id})">

the action URL ends up looking like this:

~/ProjectManagement/Milestone/Edit/39999P110175?projectChildId=1

So the route sorta works...but the action link generator doesn't? Not sure where to go from here. Any advice would be much appreciated.

Note that the same thing occurs while using @Html.ActionLink instead of @Url.Action.

Thanks!

Adam
  • 1,202
  • 11
  • 25
  • have you tried with specifying the `area` name in the routevalues too i.e. `@Url.Action("Edit", "Milestone", new { area = "ProjectManagement", projectNumber = Model.Project.ProjectNumber, projectChildId = entry.Id})`? – Russ Cam Mar 12 '12 at 15:34
  • Yea that didn't work either. Doesn't seem to be an issue with the area itself, unless maybe its conflicting somehow with a default route or something. – Adam Mar 12 '12 at 15:36
  • Can you show us all of your routes? This problem usually happens because there's more than one possible route it could generate and it's picking the wrong one. (On the incoming URL there's only one possible matching route, so that works fine.) – Michael Edenfield Mar 12 '12 at 15:36
  • 3
    in particular, do you have a route with "projectNumber" but not "projectChildId" as a parameter? – Michael Edenfield Mar 12 '12 at 15:37
  • Yea...that was it. I had a route defined previously that only had ProjectNumber. Thanks! – Adam Mar 12 '12 at 15:39
  • I would recommend using the route-debugger (http://nuget.org/packages/routedebugger) – Paul Mar 12 '12 at 15:42

2 Answers2

0

You don't seem to have specified the area name:

@Url.Action(
    "Edit", 
    new { 
        projectNumber = Model.Project.ProjectNumber, 
        projectChildId = entry.Id,
        area = "ProjectManagement" 
    }
)

Also make sure that there aren't any other routes that might conflict with this one in your area registration which should look like this:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "ProjectManagement_default",
        "ProjectManagement/{controller}/{action}/{projectNumber}/{projectChildId}",
        new { controller = "Project", action = "Home" }
    );
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Thanks to Michael for the response. Here's the answer for others.

I had:

 context.MapRoute(
            "ProjectManagement_ProjectPage",
            "ProjectManagement/{controller}/{action}/{projectNumber}",
            new { controller = "Project", action = "Home"}
 );

 ///more routes

  context.MapRoute(
             "ProjectManagement_ProjectPageSingle",
             "ProjectManagement/{controller}/{action}/{projectNumber}/{projectChildId}",
             new { controller = "Project", action = "Home" }
  );

The URL matched the ProjectPage route as well, so it took that one first. Had to flip the order, so the more specific route came first.

Thanks.

Adam
  • 1,202
  • 11
  • 25