c#中的MVC5路由

时间:2014-04-17 12:11:35

标签: c# asp.net-mvc-routing asp.net-mvc-5

我在配置路由时遇到问题。我的routeconfig如下:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Somepage", action = "Index", id = UrlParameter.Optional }
        );

现在我有两个控制器Sompage和Somepage2以及两个视图文件夹Somepage和Somepage2。 在我的layout.cshtml中,我有Somepage和Somepage2的链接。 Somepage的链接工作正常,但是Somepage2的链接不呈现。布局文件中的链接是

  @Html.ActionLink("some page on somepage2", "somepageonsomepage2", "Somepage2", new { target = "_blank" })

当我点击此链接时,它会尝试将我带到localhost / Somepage / somepageonsomepage2 当我想去localhost / Somepage2 / somepageonsomepage2

我不确定我哪里出错了。

1 个答案:

答案 0 :(得分:1)

您需要使用指定控制器的ActionLink的正确重载。默认情况下,操作链接的控制器将是视图的控制器。因此,如果您在文件夹HomeController中拥有控制器Index并查看Home,则操作链接的默认控制器将为HomeController

@Html.ActionLink("some page on somepage2", "somepageonsomepage2", "Somepage2", null, new { target = "_blank" })