ASP.NET MVC 路由:操作名称与自定义路由冲突

发布于 2024-09-30 07:24:39 字数 809 浏览 0 评论 0原文

我全部,

上一个问题中,我问如何定义自定义路由来处理以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-10-07 07:24:39

当框架要选择要执行的操作时,它首先检查具有与请求匹配的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文