4

我在 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’中方法‘System.Web.Mvc.ActionResult Index(Int32)’的不可为空类型‘System.Int32’的参数‘id’的空条目。 Controllers.ProfileController'。可选参数必须是引用类型、可空类型或声明为可选参数。”

我也想要网址

~/Members/Profile/Edit/3

为了使所有这些 URL 正常工作,我应该修改什么?

4

1 回答 1

4

您将需要在您已经定义的路线之前添加一些额外的路线。这是因为这些是您想要在您已经拥有的更通用的路线之前选择的特定路线。

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" }
     );
于 2010-09-27T09:14:45.957 回答