0

I'm new to ASP.NET MVC, and am following an online tutorial to build a sample project before I build a production project for my internship.

In this project we follow the basic MVC scaffolding, and create two different controllers. The first controller is the default HomeController, which displays a page whether the url be:

  • example.com
  • example.com/home
  • example.com/home/index

I inferred that this was because in the RouteConfig file, the defaults: argument has listed controller="home", "action="index"

However, after creating my own controller, and mapping a route to it using routes.MapRoute in the exact same format as the HomeController:

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

        routes.MapRoute(
            name: "Process",
            url: "Process/{action}/{id}",
            defaults: new { controller = "Process", action = "List", id = UrlParameter.Optional });

The new route only seems to work if I specify example.com/process/list, as opposed to defaulting to example.com/process/list if "/list" was omitted.

Is there something special in the normal HomeController template that defines the same page being returned on different URLs?

nostalgk
  • 229
  • 2
  • 20
  • See [Why map special routes first before common routes in asp.net mvc?](https://stackoverflow.com/questions/35661062/why-map-special-routes-first-before-common-routes-in-asp-net-mvc) Routing depends on the behavior that the *first match* always wins, so in order for it to work you have to constrain your routes to match only the URLs they pertain to and place them in the correct order. – NightOwl888 Jun 29 '18 at 14:48
  • Oops. It turns out my problem actually existed in both my order precedence, and a hidden " " that made it into my default controller statement, making it so even when the order precedence was correct, the typo made it non-functional. Thank you for the reading, it was helpful. – nostalgk Jun 29 '18 at 15:04

1 Answers1

0

It happens because of the order of your routes. example.com/process matches your first route ("Default"), so it gets action "Index" as default. You can swap route registrations, then it will choose "Process" route with "List" action. Read this article to get more understanding how routing works

Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
  • My issue was actually a typo, but your reading helped me fix the order precedence as well. Thank you. – nostalgk Jun 29 '18 at 15:04