这是我的代码:
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "snakes",
url: "snakes-databases/",
defaults: new { controller = "SnakesController", action = "index" }
);
routes.MapRoute(
name: "snakesLists",
url: "snakes-databases/{family}/",
defaults: new { controller = "SnakesController", action = "Lists", family = "" }
);
routes.MapRoute(
name: "snakesDetails",
url: "snakes-databases/{family}/{species}/",
defaults: new { controller = "SnakesController", action = "Details", family = "", species = "" }
);
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
}
}
_Layout.cshtml
@Html.RouteLink("snakes database", "snakes", null, new { @class = "dropdown-toggle", data_toggle="dropdown"})
<ul class="dropdown-menu">
<li>@Html.RouteLink("Boidae", "snakesLists", new { family = "boidae" })</li>
<li>@Html.RouteLink("Colubridae", "snakesLists", new { family = "colubridae" })</li>
<li>@Html.RouteLink("Elapidae", "snakesLists", new { family = "elapidae" })</li>
<li>@Html.RouteLink("Viperidae", "snakesLists", new { family = "viperidae" })</li>
</ul>
SnakeController.cs:
public ActionResult Lists(string family)
{
return View();
}
我不明白为什么每个网址除了主页(/)我都会收到404错误。
答案 0 :(得分:3)
哦,你不需要设置:
controller = "SnakesController"
只设置为:
controller = "Snakes"
全部是:
routes.MapRoute(
name: "snakes",
url: "snakes-databases/",
defaults: new { controller = "Snakes", action = "index" }
);
routes.MapRoute(
name: "snakesLists",
url: "snakes-databases/{family}/",
defaults: new { controller = "Snakes", action = "Lists", family = "" }
);
routes.MapRoute(
name: "snakesDetails",
url: "snakes-databases/{family}/{species}/",
defaults: new { controller = "Snakes", action = "Details", family = "", species = "" }
);
希望可以帮助你:)
答案 1 :(得分:0)
您没有定义您的操作,因为您不必执行此操作,因为它默认为“索引”操作。
routes.MapRoute(
name: "snakes",
url: "snakes-databases/{index}",
defaults: new { controller = "SnakesController", action = "Index" }
);
routes.MapRoute(
name: "snakesLists",
url: "snakes-databases/{Lists}/{family}/",
defaults: new { controller = "SnakesController", action = "Lists", family = "" }
);
routes.MapRoute(
name: "snakesDetails",
url: "snakes-databases/{Details}/{family}/{species}/",
defaults: new { controller = "SnakesController", action = "Details", family = "", species = "" }
);