5

我在网上寻找了这个问题的答案,但老实说,我似乎找不到 MVC 路线的好参考。

我的 User 对象有一个 UserController。可以对用户进行编辑、保存、查看等操作,因此我在该控制器中有操作来处理其中的每一个。这一切都很简单。但是我最近创建了一个新的 UserProfile 对象,它也可以编辑、查看等。我不想为 UserProfile 创建一个全新的控制器,而是想利用现有的 UserController。因此,要查看用户的个人资料,我希望 URL 为:

http://www.example.com/User/Profile/{userProfileID}

要进行编辑,我希望 URL 为:

http://www.example.com/User/Profile/Edit/{userProfileID}

UserController 中的每个操作都将返回不同的视图页面。

我将如何定义处理这种结构的路线?非常感谢。

4

1 回答 1

11

在 RegisterRoutes() 方法的 Global.asax 文件中,执行以下操作:

routes.MapRoute(
    "ProfileRoute",
    "User/Profile/{action}/{userProfileID}",
    new { controller = "User", action = "Index" });

正如评论所指出的......这必须在默认路线之前。

于 2010-11-02T17:14:25.730 回答