且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC URL路由

更新时间:2022-11-23 10:02:22

它实际上非常灵活,我想您会发现它具有更多的经验,功能非常强大.您可以按照以下方式做自己想做的事:

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:

routes.MapRoute("MyRoute", 
                 "{username}", 
                 new { controller = "Home", action = "Index", username = "" });

这将选择默认控制器(主页")和默认操作方法(索引"),并向其传递一个用户名参数(默认情况下设置为").

This chooses a default controller ("Home") and a default action method ("Index"), and passes it a username parameter (which is set to "" by default).

请谨慎使用此路由,因为它实际上会匹配您可以想象的任何URL.它应该是您添加到映射中的最后一条路线,因此您的其他路线最先出现在URL上.

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.