【问题标题】:ASP.NET MVC : Empty ActionLinks appearingASP.NET MVC:出现空的 ActionLink
【发布时间】:2012-02-13 06:01:50
【问题描述】:

我使用的是默认路由,所以我不需要指定控制器。

routes.MapRoute(
    "Default", 
    "{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

有了这个,我可以创建像 myapp.com/Customers 这样的 URL,而不是 myapp.com/Home/Customers

当我在本地测试时,一切都很好。当我上传实时版本时,使用 Html.ActionLink 生成的任何链接都是空的。我知道我正确使用了 Html.ActionLink,因为它在本地运行良好:

//                   Title                 Action      Controller
<%: Html.ActionLink("Manage My Settings", "Settings", "Home") %>

我已经删除了除默认路由之外的所有路由,尝试指定带有和不带有控制器的 ActionLink 等。我什至尝试将控制器恢复到路由中,例如:

"{controller}/{action}/{id}"

没有什么是实时的。一切都在本地工作。有点发疯。

更新:

好的,有一个奇怪的发现。我实际上在 id 之后还有另一个可选的 UrlParameter,称为 page。我愚蠢地没有将它包含在示例中,因为我认为它没有任何区别。如果我把它拿出来,一切似乎都奏效了。

所以,实际上,这是可行的:

routes.MapRoute(
   "Default", 
   "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

并且这个有效!

routes.MapRoute(
   "Default", 
   "{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

但是这个确实工作

routes.MapRoute(
   "Default", 
   "{action}/{id}/{page}", 
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional, page = UrlParameter.Optional }
);

为什么不呢?

【问题讨论】:

  • 在哪个版本的 IIS 上,您已经部署了与本地相同的应用程序?
  • 服务器上的 IIS6,以及本地运行 Visual Studio 2010 的任何内部服务器,我相信它是 IIS 7.5
  • 这是我的答案中的问题
  • 以前在 IIS6 上工作过吗?在您驳回 Serghei 的回答之前,您应该遵循这些步骤。 MVC 不能在开箱即用的 IIS 6 上运行。
  • 你能解释一下你所说的“空”是什么意思吗?链接看起来像这样吗? 管理我的设置

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


【解决方案1】:

找到答案了!在使用两个连续的可选 UrlParameters 时,MVC3 中存在一个错误,Phil Haack 对此进行了详细说明,此处为routing-regression-with-two-consecutive-optional-url-parameters

您需要首先声明一个只有一个可选参数的路由版本。所以

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

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

现在看起来真的很明显。如果我真的包含了所有细节,我相信 Serghei 或其他人会看到这个问题,所以感谢所有帮助人员!

【讨论】:

【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 2011-01-04
  • 2011-02-08
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
相关资源
最近更新 更多