MVC4默认路由

时间:2015-02-16 22:38:05

标签: asp.net-mvc asp.net-mvc-4 routes

我有以下两条路线,当用户登陆主页时,它会抛出404.是否有一些令人目眩的明显我错过了?我希望他们去家庭控制器...

        routes.MapRoute(
            name: "Default",
            url: "Home/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Coin.Web.Controllers" }
        );

        routes.MapRoute(
            name: "Organization",
            url: "{organization}/{controller}/{action}/{id}",
            defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Coin.Web.Controllers" }

提前致谢!

1 个答案:

答案 0 :(得分:0)

是的,路由/将错过“默认”路由(因为它需要在网址中设置/Home段)您设置的方式,它也会错过“组织“路由(因为您没有定义默认组织)。这就是你得到404的原因。

您可以通过显式添加主页路由来修复它:

    routes.MapRoute(
        name: "Home",
        url: "",
        defaults: new { controller = "Home", action = "Index" },
        namespaces: new[] { "Coin.Web.Controllers" }
    );

    routes.MapRoute(
        name: "Default",
        url: "Home/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "Coin.Web.Controllers" }
    );

    routes.MapRoute(
        name: "Organization",
        url: "{organization}/{controller}/{action}/{id}",
        defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "Coin.Web.Controllers" }

仅供参考 - 通常最好将更具体的路线放在不太具体的路线之前。也就是说,“组织”路线应该先行,你应该单独留下“默认”路线。但是,您需要定义一个显式段或约束来强制错过“组织”路由或使用多个组织路由以使其工作。