【问题标题】:ActionLink contains current parameters when route matchesActionLink 包含路由匹配时的当前参数
【发布时间】:2011-02-01 14:03:07
【问题描述】:

我有一个以下控制器,其中包含一个带有单个参数的操作:

public class HomeController : Controller
{
  public ActionResult Index(int? param)
  {
    return View();
  }
}

我的 MasterPage 包含一个 ActionLink,它应该指向 '/Home/Index':

Html.ActionLink("MyActionLinkText", "Index", "Home")

现在我添加这样的路线:

routes.MapRoute(
  "MyRoute",
  "Home/Something/{param}",
  new { controller = "Home", action = "Index" },
  new { param = "\\d+" });

当我导航到“/Home/Index”时,ActionLink 指向“/Home/Index”。没关系。 但是当我导航到“Home/Something/123”时,ActionLink 也指向“Home/Something/123”。

如果我以这种方式生成 ActionLink,我会理解这种行为:

Html.ActionLink("test", "Index", "Home", new { param = 123 }, null)

但由于我不提供任何路线值,我不理解这种行为。

我该怎么做才能让 ActionLink 始终指向“/Home/Index”,独立于当前参数?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您需要为“索引/主页”案例添加更具体的路线。像这样的东西可能会起作用(在你自己的“MyRoute”路由之前添加它,否则它不会被拾取):

    routes.MapRoute(
        "MyRouteNoParam",
        "Home/Index",
        new { controller = "Home", action = "Index" });
    

    【讨论】:

      【解决方案2】:

      我使用了一个空的路由变量

      Html.ActionLink("MyActionLinkText", "Index", "Home", new {param = ""})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-04
        • 2013-11-17
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多