I defined the a new route (see below). When I launch the application, I’d like by default:
- Go Home/Index
- If I go to /Customer/Detail/MyCode get “MyCode”
When I use this configuration, I start on Home/Index it’s ok but /Customer/Detail/MyCode give a null value all the time.
If I inverse the routes.MapRoute, I have :
- By default I go to /Customer/Detail and I get mycode value all the time (no null anymoe)
- I don’t start on /Home/Index
Any idea ?
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"CustomerDetail",
"{controller}/{action}/{code}",
new { controller = "Customer", action = "Detail", code = UrlParameter.Optional }
);
A single route will be enough:
When you request
Home/Indexthis will invoke theIndexaction onHomeControllerand pass null as thecodeparameter and when you requestCustomer/Detail/MyCodetheDetailaction will be invoked onCustomerControllerand passedcode=MyCode.The reason you were getting null is that both your routes are equivalent, meaning that they are of the form
Controller/Action/SomeCodewhich means that for both urls the first route is matched, but the parameter is calledidinstead ofcodeso you are gettingcode=null. Another possibility is to simply leave the default route as is:and then rename the parameter to the
Detailaction to id: