Hey All I Added two custom routes
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 });
for the urls
localhost:2110/Category/addid
&
localhost:2110/Category/SubCategory/addid
but debugger straight moves and stucks in the custom route's DetailWanted action and even on init my default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
is not called
i came across a solution on
this thing solved my problem
and then rewrote my routes as
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 }
);
with controller code as
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);
}
instead of
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");}