views:

24

answers:

1

Hi,

i have an Area called Members and the following registered routes in the MembersAreaRegistration file:

context.MapRoute(
     "Members_Profile",
     "Members/Profile/{id}",
     new { controller = "Profile", action = "Index", id = UrlParameter.Optional },
     new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" }
     );

context.MapRoute(
     "Members_default",
     "Members/{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional },
     new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" }
     );

I want to be able to map the following URLs:

~/Members (should map ~/Members/Home/Index )
~/Members/Profile/3 (should map ~/Members/Profile/Index/3)

With this route registrations everything works fine. However, I added the following URL:

~/Members/Profile/Add 

and I got the error:

"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MyProject.Web.Mvc.Areas.Members.Controllers.ProfileController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."

I also want to have the URL

~/Members/Profile/Edit/3

What should I modify in order to have all this URLs working properly?

+1  A: 

You will need to add a couple of additional routes, BEFORE the routes you have already defined. This is because these are specific routes that you want picked before the more generic routes you already have.

context.MapRoute(
     "Members_Profile",
     "Members/Profile/Add",
     new { controller = "Profile", action = "Add" },
     new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" }
     );

context.MapRoute(
     "Members_Profile",
     "Members/Profile/Edit/{Id}",
     new { controller = "Profile", action = "Edit", id = UrlParameter.Optional },
     new string[] { "MyProject.Web.Mvc.Areas.Members.Controllers" }
     );
Clicktricity
I followed your advice with a little modification. Now the routes I am using are as follows (the order is important):1. "Members/Profile/Add"2. "Members/Profile/{id}"3. "Members/{controller}/{action}/{id}" I got rid of the route for Edit, because it is covered by the last and most generic one. Thanks for your help
Martin
Glad to be of assistance. Please mark the question as answered so others can find it too.
Clicktricity