代码之家  ›  专栏  ›  技术社区  ›  Hannoun Yassir

如何使用ASP.NET MVC路由进行此操作?

  •  0
  • Hannoun Yassir  · 技术社区  · 14 年前

    有没有办法创建这样的路线” http://mysite/Username “?

    2 回复  |  直到 14 年前
        1
  •  6
  •   tvanfosson    14 年前

    对。使用路由约束创建与用户匹配的路由:

    routes.MapRoute(
                "User",                                 // Route name
                "{user}",                           // URL with parameters
                new { controller = "User", action = "Index", user = "" },  // Parameter defaults
                new { isUser = new MustBeUserConstraint() }
            );
    
    public class MustBeUserConstraint : IRouteConstraint
    {
        public bool Match
            (
                HttpContextBase httpContext, 
                Route route, 
                string parameterName, 
                RouteValueDictionary values, 
                RouteDirection routeDirection
            )
        {
            ...ensure that there is a user route value and validate that it is a user...
        }
    
    }
    
        2
  •  0
  •   Serhat Ozgel    14 年前
    routes.MapRoute(
        "RouteName",
        "{username}",
        new { controller = "SomeController", action = "SomeAction", username = "" }
    );
    
    public class SomeController : Controller
    {
        public ActionResult SomeAction(string username)
        { ... }
    }