gpt4 book ai didi

c# - asp.net mvc + routelink 只得到 404 错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:37 27 4
gpt4 key购买 nike

这是我的代码:

路由配置.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();
}

我不明白为什么除了主页 (/) 之外的每个 url 都会出现 404 错误。

最佳答案

哦,你不需要设置:

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 = "" }
);

希望能帮到你:)

关于c# - asp.net mvc + routelink 只得到 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com