Miguel Lattuada
Miguel Lattuada

Reputation: 5417

ASP.NET MVC routing: URL is falling into the wrong route

I'm working into a really common mvc Project and I have three diferents routes:

Route for display products by id

        routes.MapRoute(
            "get-by-id",
            "{controller}/{id}",
            new { action = "GetById" },
            new { id = @"\d+" }
            );

Route for display products by category

        routes.MapRoute(
            "get-by-category",
            "{controller}/{category}",
            new { action = "GetByCategory" },
            new { category = @"\w+" }
            );

Default route

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

Conslusion: I want the /products/create request falls into the default route (because I want "create" to be rendered as an action) and not into the get-by-category route (because it take "create" as a string).

Upvotes: 0

Views: 1755

Answers (1)

Gjohn
Gjohn

Reputation: 1281

Use Route Debuger it will help you debug your routes to figure out which routes are being called when. One of the tools I always nuget when working on asp.net mvc.

BTW - looking at your routes there is no route there that maps to Product/Create. In which case it is just going to take you to the default route. You need to have a route specified which is going to map to the Products controller and if you want to have an action of Create it will need to have "Products/Create" with its action pointing to Get-by-id action

routes.MapRoute(
        "get-by-id",
        "{controller}/Create/{id}",
        new { action = "GetById" },
        new { id = @"\d+" }
        );

Upvotes: 1

Related Questions