asp.net mvc路由问题
默认路由工作正常
mysite.com/home/about
我什至看到如何自定义它以使其更短,
所以我可以说:
mysite.com/edit/1 而不是 mysite.com/home/edit/1
但我怎样才能让它更长地处理像下面这样的 url
mysite.com/admin/user/1 // 有效
// 无效
mysite.com/admin/user/details // 不起作用 mysite.com/admin/user/1 // 有效mysite.com/admin/user/details com/admin/question/create // 不起作用
我不能只将 id 视为一个操作?我需要自定义路线吗?
我是否需要为每个表创建新的控制器,或者我可以通过管理控制器将它们全部路由,
非常感谢
the default routing works fine
mysite.com/home/about
and i even see how to customize it to make it shorter
so i can say:
mysite.com/edit/1
instead of
mysite.com/home/edit/1
but how can i make it longer to handle url like the following
mysite.com/admin/user/1 // works
mysite.com/admin/user/details // does not work
mysite.com/admin/question/create // does not work
i cant just treat the id as an action? i need a custom route?
do i need to create new controllers for each table or can i route them all through the Admin controller
thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如已经提到的,您最好的选择可能是使用新的 Areas< /a> 功能
您可以在没有区域的情况下实现这种类型的路由,但随着控制器数量的增加,站点的可维护性将会降低。本质上,您所做的是将控制器名称硬编码到路由定义中,这意味着您必须为每个新的管理控制器添加新的路由映射。以下是一些示例,说明您可能希望如何在没有区域的情况下设置路线。
或者,您可以通过管理控制器路由所有内容,但由于控制器操作执行多个角色,它很快就会变得非常混乱。
您的 AdminController 操作如下所示:
As has been mentioned already, probably your best bet would be to use the new Areas feature
You can achieve this type of routing without Areas, but as the number of controllers gets large the maintainability of your site will diminish. Essentially what you do is to hard-code the controller name into the Route definition which means that you have to add new route mappings for each new Admin controller. Here's a few examples of how you might want to set up your routes without Areas.
Alternatively you could route everything through the Admin controller, but it would quickly become very messy with your controller actions performing multiple roles.
With your AdminController action(s) looking like:
将路由添加到 global.asax 相当简单。将更具体的路线放在更一般的路线之上。最典型的模式是控制器/操作/参数/参数...如果您需要更复杂的东西,您可能需要查看 MVC 区域。在上面的示例中,“mysite.com/admin/user/details”正在寻找一个名为“admin”的控制器和名为“user”的操作,之后的所有内容都是操作方法上的参数(假设典型的路由设置)
Adding routes to global.asax is fairly straight forward. Put the more specific routes above the more general routes. The most typical pattern is controller/action/parameter/parameter... If you need something more complex, you may want to look at MVC Areas.In you example above "mysite.com/admin/user/details" is looking for a controller named "admin" and an action named "user", with everything after that being parameter on the action method (assuming a typical route setup)