Asp.net mvc 4 具有多个参数的MVC路由无效
嘿,我添加了两条自定义路线Asp.net mvc 4 具有多个参数的MVC路由无效,asp.net-mvc-4,routing,asp.net-mvc-routing,Asp.net Mvc 4,Routing,Asp.net Mvc Routing,嘿,我添加了两条自定义路线 routes.MapRoute( "ParentCat", "{PCat}/{id}", new { Controller = "Adds", Action = "DetailWanted", PCat = UrlParameter.Optional, id = UrlParameter.Optional }); routes.MapRoute( "SubCat",
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 });
对于URL
localhost:2110/Category/addid
&
但是调试器直接在自定义路由的detailwant操作中移动和阻塞,甚至在init my default route上
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");}
多个可选参数可能不可能,但任何一个都可以提供解决方案或替代方案
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");}