ASP.NET MVC 路由:操作名称与自定义路由冲突
我全部,
在上一个问题中,我问如何定义自定义路由来处理以下 URL:
http://www.example.com/User/Profile/Edit/{userProfileID}
我有一个 User 对象和一个 UserProfile 对象,但只有一个我希望能够用于对这两个对象执行操作的 UserController。我已经在 UserController 中有一个名为 Edit 的方法来处理用户的编辑。但我还需要一种编辑用户配置文件的方法。我的路由问题的答案是以下路由:
routes.MapRoute(
"ProfileleRoute", // Route name
"User/Profile/{action}/{userProfileID}", // URL with parameters
new { controller = "User", action = "Index" } // Parameter defaults
);
但是考虑到该自定义路由,我应该在哪里声明 UserProfile 的编辑操作,以及应该调用什么?似乎我无法在 UserController 中编写另一种名为 Edit 的方法,因为我已经有一个处理用户编辑的方法。
所以我觉得我最终需要两个编辑操作来处理以下路由:“用户/编辑”和“用户/配置文件/编辑”。我该如何解决这个问题?
非常感谢。
i all,
In a previous question, I asked how to define a custom route to handle the following URL:
http://www.example.com/User/Profile/Edit/{userProfileID}
I have a User object and a UserProfile object, but only a UserController that I want to be able to use for actions on both objects. I already have a method in UserController called Edit that handles edits on a User. But I also need a method for edits on a UserProfile. The answer to my routing question was the following route:
routes.MapRoute(
"ProfileleRoute", // Route name
"User/Profile/{action}/{userProfileID}", // URL with parameters
new { controller = "User", action = "Index" } // Parameter defaults
);
But given that custom route, where should I be declaring the edit action for a UserProfile, and what should it be called? It seems like I couldn't write another method in UserController called Edit because I already have one that handles User edits.
So I feel like I would end up with a need for two Edit actions to handle the following routes: "User/Edit" and "User/Profile/Edit". How do I get around this?
Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当框架要选择要执行的操作时,它首先检查具有与请求匹配的 HttpPost 或 HttpGet 属性所需名称的操作,如果没有以这种方式选择操作,那么它会选择与该名称匹配的任何操作。
因此,如果您有两个同名的操作,但没有 HttpPost 或 HttpGet 属性,则您无法控制操作的执行。
When the framework it's going to select what action to execute it first check the actions with the name required with a HttpPost ot HttpGet attribute that match the request, if not action is selected this way, then it select any action that match the name.
So, if you have two actions with the same name with no HttpPost or HttpGet attributes, you can't control with action is going to get executed.