0

Is there a way to create a route like this "http://mysite/Username" ?

2
  • Where's this Username coming from? Forms authentication? Commented Mar 1, 2010 at 12:41
  • a user is stored in database with some related information. Commented Mar 1, 2010 at 12:44

2 Answers 2

6

Yes. Create a route that matches a user using a routing constraint:

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...
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0
routes.MapRoute(
    "RouteName",
    "{username}",
    new { controller = "SomeController", action = "SomeAction", username = "" }
);

public class SomeController : Controller
{
    public ActionResult SomeAction(string username)
    { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.