Debsankar Patra

Debsankar Patra

  • NA
  • 32
  • 4.2k

Why In registerroute method we need to maintain sequence?

Oct 21 2015 2:42 AM

Why In registerroute method we need to maintain sequence?

i,e default route mapping should be placed at the last .Say i have two url routing(Map1, Map2) as below

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

//Map 1

routes.MapRoute(

"CustomUrm",

"MyUrl/{Category}/{Id}", // URL with parameters //

new { controller = "Home", action = "MyAction", Category = "xyz", Id = 2 });

//Map 2

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new { controller = "Home", action = "Index", id = UrlParameter.Optional} );

}

By default Controller "Home" with action method "Index" wil be called while running the application(Say in URL its"http://localhost:63263/")

Then in url i am typing "http://localhost:63263/MyUrl" then its calling action method "MyAction" of controller "Home".

But if i am changing the url mapping sequence(Map1 and Map2) then with URL "http://localhost:63263/MyUrl" it is showing error : resource not found, Requested URL: /MyUrl.

Thanks

Answers (7)