Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC User routing like that in StackOverflow?

I've looked at the routing on StackOverflow and I've got a very noobie question, but something I'd like clarification none the less.

I'm looking specifically at the Users controller

https://stackoverflow.com/Users
https://stackoverflow.com/Users/Login
https://stackoverflow.com/Users/124069/rockinthesixstring

What I'm noticing is that there is a "Users" controller probably with a default "Index" action, and a "Login" action. The problem I am facing is that the login action can be ignored and a "UrlParameter.Optional [ID]" can also be used.

How exactly does this look in the RegisterRoutes collection? Or am I missing something totally obvious?

EDIT: Here's the route I have currently.. but it's definitely far from right.

    routes.MapRoute( _
        "Default", _
        "{controller}/{id}/{slug}", _
        New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional} _
    )
like image 647
Chase Florell Avatar asked Jun 08 '10 19:06

Chase Florell


People also ask

How does ASP.NET MVC routing work?

ASP.NET MVC Routing does the same thing; it shows the way to a request. Basically, routing is used for handling HTTP requests and searching matching action methods, and then executing the same. It constructs outgoing URLs that correspond to controller actions. Routing the map request with Controller's Action Method.

Can we have multiple routing in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

How routing is done in the MVC pattern?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.


1 Answers

Probably just uses a specific route to handle it, also using a regex to specify the format of the ID (so it doesn't get confused with other routes that would contain action names in that position).

// one route for details
routes.MapRoute("UserProfile",
     "Users/{id}/{slug}",
     new { controller = "Users", action = "Details", slug = string.Empty },
     new { id = @"\d+" }
);
// one route for everything else
routes.MapRoute("Default",
     "{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
like image 131
John Nelson Avatar answered Oct 03 '22 11:10

John Nelson