Determining the name of the action method based on request values

Viewed 33

As I understand in Asp.Net Mvc requests to the server get mapped to a controller and action. For instance if you have stackoverflow.com/questions/ask in the address bar that means that the request is mapped to the questions controller and the ask action method.

My question is how if you have a private area in your site for the clients of your site. How would you write that using this paradigm? Meaning if I have a stack overflow account when I log in to my personal area it says in the address bar stack overflow.com/users a number and my user name. How do you write a action whose name will be the userName of the user? Another words how do you write a method without a name whose name will be determined by the user

1 Answers

You can set the priority of routing:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "questionsRoute",
                url: "Questions/Ask/{id}",
                defaults: new { controller = "Question", action = "Ask", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "users/{id}",
                defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
Related