ASP.NET MVC中的路由,显示URL中的用户名

我试图建立一个路线,所以我可以在URL中显示用户名,如下所示:

HTTP:// localhost1234 /约翰·

这是我的routeconfig:

routes.MapRoute( name: "users", // Route name url: "{username}", // URL with parameters defaults: new { controller = "Home", action = "Index", username = "" } // Parameter defaults ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

这是我的HomeController:

  public ActionResult Index(string username = "Test") { return View(); } 

首先,URL未更改。 当我在route-config中设置username = "Test" ,URL不会更改。

其次,我无法导航到我的其他控制器。 如果我将URL更改为http:// localhost123 / Welcome ,则不会发生任何事情。 它应该将我重定向到一个新页面。

我在这做错了什么?

如果我更改路由的顺序,我可以导航到其他页面,但用户名不显示在URL中。

我用谷歌搜索过,关于这个主题的所有答案都说我应该使用像上面那样的路线。

如果url是.../Product意味着您想要导航到ProductControllerIndex()方法,它将与您的第一个路径相匹配(并假设“Product”是username 。您需要为您的路由定义添加路由约束,如果username有效则返回true否则返回false (在这种情况下,它将尝试以下路由来查找匹配)。

假设您有一个具有以下方法的UserController

 // match http://..../Bryan public ActionResult Index(string username) { // displays the home page for a user } // match http://..../Bryan/Photos public ActionResult Photos(string username) { // displays a users photos } 

然后你需要路由定义

 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "User", url: "{username}", defaults: new { controller = "User", action = "Index" }, constraints: new { username = new UserNameConstraint() } ); routes.MapRoute( name: "UserPhotos", url: "{username}/Photos", defaults: new { controller = "User", action = "Photos" }, constraints: new { username = new UserNameConstraint() } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional } ); } public class UserNameConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { List users = new List() { "Bryan", "Stephen" }; // Get the username from the url var username = values["username"].ToString().ToLower(); // Check for a match (assumes case insensitive) return users.Any(x => x.ToLower() == username); } } } 

如果url是.../Bryan ,它将匹配User路由,你将在UserController执行Index()方法(并且username的值将是"Bryan"

如果url是.../Stephen/Photos ,它将匹配UserPhotos路由,你将在UserController执行Photos()方法( username的值将是"Stephen"

如果url是.../Product/Details/4 ,那么前两个路由定义的路由约束将返回false,您将执行ProductControllerDetails()方法

如果url是.../Peter.../Peter/Photos并且没有用户username = "Peter"那么它将返回404 Not Found

请注意,上面的示例代码对用户进行了硬编码,但实际上您将调用一个返回包含有效用户名的集合的服务。 为避免在每个请求中访问数据库,您应该考虑使用MemoryCache来缓存集合。 代码首先检查它是否存在,如果没有填充,则检查集合是否包含username 。 如果添加了新用户,您还需要确保缓存无效。

您需要为网站的不同部分对url进行分类,以便url模式匹配机制顺利进行。 例如,在您的情况下,将类别“个人资料”或其他任何内容放入。 现在你的请求url看起来像http:// localhost1234 / profile / john和路由

  routes.MapRoute( name: "users", // Route name url: "Profile/{username}", // URL with parameters defaults: new { controller = "Home", action = "Index" } // Parameter defaults ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

有关更多信息,请参阅MVC中的路由链接