user2227904 February 2016

MVC Route not found although declared

I got the following routes declared:

        routes.MapRoute(
             name: "SurveyContent",
             url: "Survey/{action}/{id}",
             defaults: new { controller = "Contents", action = "List", id = 3, type = 3 }
         );

         );                
        routes.MapRoute(
             name: "ItemCreation",
             url: "{controller}/Create",
             defaults: new { controller = "Contents", action = "Details", id = 0 }
         );
        routes.MapRoute(
             name: "OpenContent",
             url: "Open/{code}",
             defaults: new { controller = "Courses", action = "OpenContent", code = "" }
         );

I am trying to access the link localhost/Survey/Create, but returns me a 404 error. Later on I added

        routes.MapRoute(
             name: "SurveyContent1",
             url: "Survey/Create",
             defaults: new { controller = "Contents", action = "Details", id = 0, type = 3 }
         );

under the SurveyContent, but that didn't change anything. What am I doing wrong?

Answers


NikolaiDante February 2016

The route:

url: "Survey/{action}/{id}",

Will match Survey/Create, before:

url: "{controller}/Create",

So place ItemCreation before SurveyContent.

In the route table, order matters.

Post Status

Asked in February 2016
Viewed 2,275 times
Voted 12
Answered 1 times

Search




Leave an answer