A
A
Arthur2016-11-28 14:45:26
ASP.NET
Arthur, 2016-11-28 14:45:26

How to implement routing in the form of domain.com/users and domain.com/user/123?

Hello, I hope that someone will tell you the solution to this problem.
There is a UsersController controller, with Index() and User(int id) methods, at the moment the routing is configured as follows:

routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "Proj.Controllers" }
            );

(1)domain.com/users will query the Index() method of the UsersController.
(2)domain.com/users/user/123 will query the User(int id) method of the UsersController.
How to make domain.com/user/123 issue a (2) action? Considering that the UserController will not exist in the project?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2016-11-28
@r3st1k25

routes.MapRoute(
    "UsersGeneric",
    "{action}/{id}",
    new { 
      controller = "Users",
      action = "User",
      id = UrlParameter.Optional
    },
    new[] { controller = "Users", [email protected]"\d+" },
    new[] { "Proj.Controllers" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { 
        controller = "Home", 
        action = "Index", 
        id = UrlParameter.Optional 
    },
    new[] { "Proj.Controllers" }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question