Search code examples
asp.net-mvc-3routesmaproute

how to map a Route like (site.com/username) in 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


Solution

  • Maybe add custom route at the very beginning that would inherit from System.Web.Routing.Route, and override method

    protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, 
       string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    

    where you would check in db using indexed column.

    Just add some not nullable constraint object like

    Constraints = new { username = "*" }
    

    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.

    public class UserRoute : Route
    {
        public UserRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler)
        {
        }
    
        protected override bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.UrlGeneration)
                return true;
    
            object value;
            values.TryGetValue(parameterName, out value);
            string input = Convert.ToString(value, CultureInfo.InvariantCulture);
            // check db
            if (!CheckHeuristicIfCanBeUserName(input))
            {
                return false;
            }
    
            // check in db if exists, 
            // if yes then return true
            // if not return false - so that processing of routing goes further
            return new[] {"Javad_Amiry", "llapinski"}.Contains(input);
        }
    
        private bool CheckHeuristicIfCanBeUserName(string input)
        {
            return !string.IsNullOrEmpty(input);
        }
    }
    

    This add at the beginning.

    routes.Add(new UserRoute("{username}", 
                new RouteValueDictionary(){ {"controller", "Home"}, {"action", "About"}}, 
                new RouteValueDictionary(){ { "username", "*" }}, new MvcRouteHandler()));
    

    Works for me in both ways, generating and incomming.