如何在 MVC 3 中映射类似 (site.com/username) 的路由
我想绘制这样的路线:
mysite.com/用户名
ASP.NET MVC 3 应用程序中的 ;请问我该怎么做? 谢谢大家,问候
I want to map a route like this:
mysite.com/username
in ASP.NET MVC 3 application; How can I do it, please?
Thanks to all, regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许在一开始就添加从 System.Web.Routing.Route 继承的自定义路由,并覆盖
使用索引列检查数据库的方法。
只需添加一些不可为空的约束对象,
以便路由对象将处理约束。
在访问数据库之前,可以先试探一下这是否可以是用户名。请记住,您不能拥有与控制器相同的用户(当具有默认操作时),因此您希望以某种方式限制它。请注意这是否不会影响年度表现,但斜线后可能没有很多具有单一值的路线。
您可以这样做。
这个加在开头。
对我来说有两种方式:产生和接收。
Maybe add custom route at the very beginning that would inherit from System.Web.Routing.Route, and override method
where you would check in db using indexed column.
Just add some not nullable constraint object like
so that route object will process constraints.
Before hitting database maybe make some heuristics whether this can be username. Remember you cant have user same as controller (when having default action), so you want to limit that somehow. Pay attention if that wont hurt year performance, but you probably dont have many routes with single value after slash.
This is how you can do this.
This add at the beginning.
Works for me in both ways, generating and incomming.
也许是这样的?
当你的控制器是
Maybe something like this?
With your controller being