1

I want to map multiple urls to 1 action, but I just want 1 route.

For eg.

routes.MapRoute(
    "SingleUser_Long",
    "Users/{username}",
    new { controller = "Users", action = "SingleUser" });

routes.MapRoute(
    "Users",
    "{username}",
    new { controller = "Users", action = "SingleUser" });

Is there a way I can put these 2 in 1? Its becomming tedious when I have to add more and I keep copy/paste duplicates, only to add "Users" infront of the url.

2
  • You want to add 'Users' infront of the url? Doesn't this automatically happen because your controller is named 'Users'? Commented Sep 20, 2011 at 22:13
  • Right now the Urls 'localhost/Users/1' and 'localhost/1' maps to SingleUser action. But as you can see, it required 2 MapRoute, is it possible to do in 1? Commented Sep 20, 2011 at 23:37

1 Answer 1

1
    routes.MapRoute("SingleUser", 
      "{controller}/{username}",
       new { controller = "Users", action = "SingleUser", 
                 controller = UrlParameter.Optional },
       new { controller = @"/(^\s*)|\bUsers\b"}

using constraint to restrict "{controller}" to be empty or "Users" only.(not sure the regular expression is right)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.