简体   繁体   English

asp.net mvc + routelink只收到404错误

[英]asp.net mvc + routelink get only 404 error

here's my code: 这是我的代码:

RouteConfig.cs 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 _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: SnakeController.cs:

public ActionResult Lists(string family)
{
    return View();
}

I do not understand why for every url except homepage (/) I get a 404 error. 我不明白为什么每个网址除了主页(/)我收到404错误。

Oh you no need to set: 哦,你不需要设置:

controller = "SnakesController"

only set it is: 只设置它是:

controller = "Snakes"

All is: 一切都是:

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

Hope can help you :) 希望可以帮到你:)

You are not definening your actions, for the homepage you dont have to do it because that's default the Index action. 您没有定义您的操作,对于您不必执行此操作的主页,因为这是默认的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 = "" }
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM