这是我的代码:
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错误。
最佳答案
您无需定义操作,对于首页,您无需执行操作,因为这是默认的Index操作。
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 = "" }
);
关于c# - asp.net MVC + RouteLink仅收到404错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32757536/