ASP.NET MVC 路由:如何定义自定义路由
我在网上寻找这个问题的答案,但老实说,我似乎找不到 MVC 路线的良好参考。
我有一个用于我的 User 对象的 UserController 。人们可以对用户进行编辑、保存、查看等操作,因此我在该控制器中具有操作来处理每一项操作。这一切都非常简单。但我最近创建了一个新的 UserProfile 对象,也可以编辑、查看等。我不想仅为 UserProfile 创建一个全新的控制器,而是想利用现有的 UserController。因此,要查看用户的个人资料,我希望 URL 为:
http://www.example.com/User/Profile/{userProfileID}
要编辑,我希望 URL 为:
http://www.example.com/User/Profile/Edit/{userProfileID}
UserController 中的每个操作都将返回不同的视图页面。
我将如何定义路由来处理这个结构?非常感谢。
I've looked online for an answer to this question, but I honestly can't seem to find a good reference for MVC routes.
I have a UserController for my User objects. One can Edit, Save, View, etc. on the User, so I have actions in that controller to handle each of those. That's all pretty straightforward. But I've recently created a new UserProfile object that one can also edit, view, etc. Rather than create an entirely new controller just for the UserProfile, I'd like to make use of the existing UserController. So to view a user's profile, I'd like the URL to be:
http://www.example.com/User/Profile/{userProfileID}
And to edit, I'd like the URL to be:
http://www.example.com/User/Profile/Edit/{userProfileID}
Each of these actions in the UserController will return a different view page.
How would I go about defining routes to handle this structure? Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Global.asax 文件中的 RegisterRoutes() 方法中执行以下操作:
正如注释所指出的...这必须出现在默认路由之前。
In your Global.asax file in the RegisterRoutes() method do the following:
As pointed out by the comments...this must come BEFORE the Default route.