url 中没有控制器和操作的路由
如何配置 ASP.NET MVC 3 路由,使其不需要 url 中的控制器和操作?
我用它来显示项目“abc123”的详细信息,并使用默认路由设置进行配置:
mysite.com/Home/Details/abc123
值“abc123”是一个唯一值,家庭控制器使用它来在数据库中查找正确的项目。
但我更喜欢使用这些超短网址:
mysite.com/abc123
该网站还将有很少的附加控制器,例如“关于”和“联系方式”。我假设我还必须专门配置这些,因此默认控制器不会开始查找 ID 为“关于”或“联系人”的项目的详细信息。我该怎么做?
更新:
这就是我的路线最终的样子:
routes.MapRoute("About", "About", New With {.controller = "About", .action = "Index"})
routes.MapRoute("ID", "{id}", New With {.controller = "Home", .action = "Details"})
routes.MapRoute("Default", "", New With {.controller = "Home", .action = "Index"})
:)
How do I configure ASP.NET MVC 3 routing so it doesn’t require the controller and action in the url?
I have this to display the details for item “abc123”, configured with the default routing-settings:
mysite.com/Home/Details/abc123
The value "abc123" is a unique value which the Home controller uses to look up the correct item in a database.
But I would prefer to have these ultra-short urls instead:
mysite.com/abc123
The site will also have very few additional controllers, such as “About” and “Contact”. I assume I would have to configure those specifically as well, so the default controller doesn’t start looking for details for an item with ID “About” or “Contact”. How do I do that?
UPDATE:
Here's what my routes ended up looking like:
routes.MapRoute("About", "About", New With {.controller = "About", .action = "Index"})
routes.MapRoute("ID", "{id}", New With {.controller = "Home", .action = "Details"})
routes.MapRoute("Default", "", New With {.controller = "Home", .action = "Index"})
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的操作:
第一个路由应确保您可以将 /Site/About 和 /Site/Contact 分别映射到 AboutController 和 ContactController。重要的是首先要对此进行映射。
第二条路线将确保 /abc123 映射到 ID 为“abc123”的详细信息操作
Try something like this:
The first route should ensure that you can have /Site/About and /Site/Contact mapped to AboutController and ContactController respectively. It is important that this gets mapped first.
The second route will make sure that /abc123 gets mapped to the details action with id "abc123"
你可以用这个。
你的行动看起来像:
you can use this.
and your action looks like:
您还可以执行以下操作:
我正在使用 MVC3,它对我有用。
You can also do something like this:
I'm using MVC3 and it works for me.