route parameter net mvc attribute asp asp.net-mvc-4 routing asp.net-mvc-routing

asp.net mvc 4 - parameter - El enrutamiento MVC con múltiples parámetros no funciona



route mvc (1)

encontré una solución en

esto solucionó mi problema

y luego reescribí mis rutas como

routes.MapRoute( name: "SubCat", url: "{PCat}/{SCat}/{id}", defaults: new { Controller = "Adds", Action = "Details" });//, id = UrlParameter.Optional PCat = UrlParameter.Optional, SCat = UrlParameter.Optional, routes.MapRoute( name: "ParentCat", url: "{PCat}/{id}", defaults: new { Controller = "Adds", Action = "Details" });//,PCat = UrlParameter.Optional,id = UrlParameter.Optional routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

con código de controlador como

public ActionResult Details(string PCat = null, string SCat = null, int id = 0) { Add add = new Add(); if (PCat == null && SCat == null && id > 0 && id != null) { add = db.Adds.Single(a => a.AddId == id); } if (SCat == null && PCat != null && id > 0 && id != null) { add = db.Adds.Single(a => a.AddId == id && a.Category.CategoryName == PCat); } if (SCat != null && PCat != null && id > 0 && id != null) { add = db.Adds.Single(a => a.AddId == id && a.Category.CategoryName == PCat && a.Category1.CategoryName == SCat); } if (add == null) { return HttpNotFound(); } return View(add); }

en lugar de

public ActionResult DetailWanted(string PCat=null,string SCat=null, int id=0) { if (PCat == "Adds" || PCat == null) { return RedirectToAction("Index", "Home"); } if (id > 0 && id != null) { if (SCat != null && PCat != null) { //return RedirectToAction("Details", "Adds" , new { @id = id }); return Redirect("/Adds/Details/" + id); } else { return RedirectToAction("Details", "Adds" , new { @id = id }); } } else { return RedirectToAction("Index"); } return RedirectToAction("Index", "Home");}

Hola, todos, agregué dos rutas personalizadas

routes.MapRoute( "ParentCat", "{PCat}/{id}", new { Controller = "Adds", Action = "DetailWanted", PCat = UrlParameter.Optional, id = UrlParameter.Optional }); routes.MapRoute( "SubCat", "{PCat}/{SCat}/{id}", new { Controller = "Adds", Action = "DetailWanted", PCat = UrlParameter.Optional, SCat = UrlParameter.Optional, id = UrlParameter.Optional });

para las urls

localhost:2110/Category/addid

&

localhost:2110/Category/SubCategory/addid

pero depurar movimientos rectos y bloqueos en la acción DetailWanted de la ruta personalizada e incluso en init mi ruta predeterminada

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

no se llama