Asp.net mvc 2 区域内的自定义布线

Asp.net mvc 2 区域内的自定义布线,asp.net-mvc-2,asp.net-mvc-routing,asp.net-mvc-areas,Asp.net Mvc 2,Asp.net Mvc Routing,Asp.net Mvc Areas,我在MembersAreaRegistration文件中有一个名为Members的区域和以下注册路由: context.MapRoute( "Members_Profile", "Members/Profile/{id}", new { controller = "Profile", action = "Index", id = UrlParameter.Optional }, new string[] { "MyProject.Web.Mvc.Areas

我在MembersAreaRegistration文件中有一个名为Members的区域和以下注册路由:

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" }
     );
我希望能够映射以下URL:

~/Members (should map ~/Members/Home/Index )
~/Members/Profile/3 (should map ~/Members/Profile/Index/3)
有了这条路线登记,一切都很顺利。但是,我添加了以下URL:

~/Members/Profile/Add 
我得到了一个错误:

参数字典包含“MyProject.Web.Mvc.Areas.Members.Controller.ProfileController”中方法“System.Web.Mvc.ActionResult Index(Int32)”的不可为空类型“System.Int32”的参数“id”的空项。可选参数必须是引用类型、可为空类型或声明为可选参数

我还想有网址

~/Members/Profile/Edit/3

我应该修改什么才能使所有这些URL正常工作?

在定义路由之前,您需要添加两个额外的路由。这是因为这些是您希望在现有更通用的管线之前拾取的特定管线

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

我听从了你的建议,稍作修改。现在我使用的路线如下(顺序很重要):1。“成员/简介/添加”2。“Members/Profile/{id}”3。“Members/{controller}/{action}/{id}”我去掉了用于编辑的路由,因为它被最后一个也是最通用的路由覆盖。谢谢你的帮助,很高兴能提供帮助。请将问题标记为已回答,以便其他人也能找到。