gpt4 book ai didi

asp.net-mvc-2 - 区域内的自定义路由

转载 作者:行者123 更新时间:2023-12-02 14:27:57 25 4
gpt4 key购买 nike

我有一个名为 Members 的区域,并在 MembersAreaRegistration 文件中注册了以下路线:

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 

我得到了错误:

"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."

我也想要网址

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

关于asp.net-mvc-2 - 区域内的自定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3802240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com