嘿所有我添加了两个自定义路线
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 });
为网址
localhost:2110/Category/addid
&安培;
localhost:2110/Category/SubCategory/addid
但调试器直接移动并卡在自定义路由的DetailWanted操作中,甚至在init我的默认路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
未被称为
答案 0 :(得分:0)
我遇到了
的解决方案然后重写我的路线
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 }
);
控制器代码为
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);
}
而不是
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");}