ASP.Net MVC5: Extra segment in routing causes issue

Feb 9 2018 8:01 AM
to handle my routing issue i add extra segment in routing which not redirecting me to edit action.
see my routing which causing problem
  1. routes.MapRoute  
  2. (  
  3. name: "PageWithId",  
  4. url: "Customers/Action/Edit/{page}/{id}",  
  5. defaults: new { controller = "Customers", action = "Edit" }  
  6. );  
  7.   
  8. OR  
  9.   
  10. routes.MapRoute  
  11. (  
  12. name: "PageWithId",  
  13. url: "Customers/Edit/Action/{page}/{id}",  
  14. defaults: new { controller = "Customers", action = "Edit" }  
  15. );  
i test above 2 different set of routing for PageWithId but none work
see RouteLink code
  1. @Html.RouteLink("Edit""PageWithId",  
  2. new  
  3. {  
  4. controller = "Customers",  
  5. action = "Edit",  
  6. id = item.CustomerID,  
  7. page = ViewBag.CurrentPage  
  8. })  
  9. my edit action code  
  10. public ActionResult Edit(int page, string id)  
  11. {  
  12. if (id == null)  
  13. {  
  14. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  15. }  
  16. Customer customer = db.Customers.Find(id);  
  17. if (customer == null)  
  18. {  
  19. return HttpNotFound();  
  20. }  
  21. ViewBag.CurrentPage = page;  
  22. return View(customer);  
  23. }  
now tell why this url http://localhost:2020/Customers/Action/Edit/1/AlFAKI not redirecting me to edit action?
see my full routing code
  1. routes.MapRoute(  
  2. name: "PageWithSort",  
  3. url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",  
  4. defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }  
  5. );  
  6.   
  7.   
  8. routes.MapRoute  
  9. (  
  10. name: "PageWithId",  
  11. url: "Customers/Action/Edit/{page}/{id}",  
  12. defaults: new { controller = "Customers", action = "Edit" }  
  13. );  
  14.   
  15. OR  
  16.   
  17. routes.MapRoute  
  18. (  
  19. name: "PageWithId",  
  20. url: "Customers/Edit/Action/{page}/{id}",  
  21. defaults: new { controller = "Customers", action = "Edit" }  
  22. );  
  23.   
  24. routes.MapRoute(  
  25. name: "Default",  
  26. url: "{controller}/{action}/{id}",  
  27. defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  28. );  

Answers (2)