控制器前缀Url.Action中的子文件夹

时间:2016-04-14 06:38:31

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

我有下一个结构:

Controllers
   Epic
     PmController.cs
   HomeController.cs

在我的App_Start \ RouteConfig.cs中我添加了:更新 - 添加完整方法

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

但不知何故@ Url.Action总是以“Epic”为前缀。我在其他视图/控制器中使用@ Url.Action,如下所示:

<a href="@Url.Action("Links", "Home")">Links</a></li> // generates /Epic/Home/Links instead of just /Home/Links

在添加此子文件夹和路线之前,LInks工作正常。

此外,可以在控制器中使用子文件夹,也可以始终使用Areas

1 个答案:

答案 0 :(得分:1)

好像你删除了默认路由,只需在RouteConfig文件中再次添加它:

routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
        );