ASP.NET MVC路线:如何定义自定义路线 [英] ASP.NET MVC Routes: How to define custom route

查看:155
本文介绍了ASP.NET MVC路线:如何定义自定义路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找了一个回答这个问题,但老实说,我似乎无法找到MVC路线了很好的参考。

I've looked online for an answer to this question, but I honestly can't seem to find a good reference for MVC routes.

我有我的用户对象UserController的。一个可以编辑,保存,查看等方式对用户,所以我必须在控制器来处理每这些行动。这是所有pretty简单。不过,我最近创建了一个新的用户配置对象,人们还可以编辑,视图等,而不是创建一个全新的控制器,只为用户配置,我想利用现有的UserController中。因此,要查看用户的个人资料,我想的网址是:

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}

每个在UserController的措施都将返回不同的视图页面。

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.

推荐答案

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

In your Global.asax file in the RegisterRoutes() method do the following:

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

正如评论指出...这一定要来的默认路由了。

As pointed out by the comments...this must come BEFORE the Default route.

这篇关于ASP.NET MVC路线:如何定义自定义路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆