区域内的自定义路由
我有一个名为 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
并收到错误:
“参数字典包含“MyProject.Web.Mvc.Areas”中方法“System.Web.Mvc.ActionResult Index(Int32)”的不可空类型“System.Int32”的参数“id”的空条目.Members.Controllers.ProfileController'。可选参数必须是引用类型、可为 null 的类型,或者声明为可选参数。”
我还想要 URL
~/Members/Profile/Edit/3
我应该修改什么才能使所有这些 URL 正常工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在已经定义的路由之前添加一些额外的路由。这是因为这些是您希望在已有的更通用路线之前选择的特定路线。
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.