User routing within the area

I have 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)

When registering this route, everything works fine. However, I added the following URL:

~/Members/Profile/Add 

and I got the error:

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

I also want to have a URL

~/Members/Profile/Edit/3

What do I need to change for all of these URLs to work correctly?

+3
1

, , . , , , .

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" }
     );
+3

Source: https://habr.com/ru/post/1766715/


All Articles