asp.net mvc url 路由
如何映射诸如domain.com/用户名之类的内容?问题是我认为 MVC 路由会寻找控制器来确定它应该如何处理映射请求。
我对 ASP.NET MVC 还很陌生。
然而,根据迄今为止的教程,路由机制似乎相当僵化。
How do I map something like domain.com/username? The problem is I think that the MVC routing looks for the controller to determine how it should handle the mapping request.
I am pretty new to ASP.NET MVC.
However, based on the tutorials so far, the routing mechanism seems rather rigid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它实际上非常灵活,我想如果您有更多的使用经验,您会发现它非常强大。您可以按照以下方式执行您想要的操作:
这将选择一个默认控制器(“Home”)和一个默认操作方法(“Index”),并向其传递一个用户名参数(默认情况下设置为“”)。
但要小心这条路由,因为它几乎会匹配您能想象到的任何 URL。它应该是您添加到映射中的最后一个路由,因此您的其他路由首先有机会访问该 URL。
It's actually quite flexible, I think you'll find it's quite powerful with some more experience with it. Here's how you can do what you want:
This chooses a default controller ("Home") and a default action method ("Index"), and passes it a username parameter (which is set to "" by default).
Careful with this route though, because it will match virtually any URL you can imagine. It should be the last route that you add to your mappings, so your other routes have a chance at the URL first.
要添加到 womp - 我会走这条路线(双关语):
这会将 url 映射到:domain.com/user/{username}
并且您可以随时将控制器和操作更改为您想要的任何内容。
To add to womp - I would go this route (pun intended):
This will map urls to: domain.com/user/{username}
And you can always change the controller and action to whatever you want.
如果我有类似的内容,那么我可以指向 mydomain.com/userid 以及 mydomain.com/home/index/1 之类的内容。但是,如果我只是访问 mydomain.com,它会转到用于 userid 的页面,这是有道理的,因为它将它与第一个路由规则匹配并认为 id 是“”,但我该如何阻止这种行为呢?
If I have something like that, then I am able to point to something like mydomain.com/userid as well as mydomain.com/home/index/1. However, if I just go to mydomain.com, it is going to the page used for userid, which makes sense, because it matches it with the first route rule and thinks the id is "", but how do i stop that behavior?