嘿所有我添加了两个自定义路线
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
但调试器直移动和stucks在自定义路线的DetailWanted行动,甚至在初始化我的默认路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
不叫
我碰到一个解决方案
这件事情解决了我的问题
然后重写我作为航线
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");}