Question

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?

Was it helpful?

Solution

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" }
     );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top