【问题标题】:Workarounds for strongly-typed ActionLink in ASP.NET MVCASP.NET MVC 中强类型 ActionLink 的解决方法
【发布时间】:2011-06-15 12:47:27
【问题描述】:

鉴于强类型 ActionLink 不可行(Strongly Typed ActionLink In Asp.Net MVC 2?),我们不能这样做:

<%: Html.ActionLink<HomeController>(x => x.Index(), "Home") %>

我正在考虑使用static readonly string 类或资源文件作为解决方法。这将获得一定程度的编译时间检查,允许在不使用查找/替换的情况下重命名,甚至(在某些疯狂的世界中)允许本地化。不利的一面是,它需要有关可用控制器和操作的重复信息才能工作。

<%: Html.ActionLink(Controllers.Home, HomeActions.Index, HomeActions.IndexTitle) %>

有什么其他方法可以改进使用魔术字符串的不优雅默认设置吗?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    T4MVC 会为你做这件事。

    【讨论】:

      【解决方案2】:

      您可以使用T4 Templates。或者MVCContrib 包含帮助程序,可以让您拥有与您在问题中显示的完全相同的语法:

      <%: Html.ActionLink<HomeController>(x => x.Index(), "Home") %>
      

      【讨论】:

      • @Daniel A. White,太好了,很高兴我们在同一波浪潮中 :-)
      猜你喜欢
      • 1970-01-01
      • 2010-09-18
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多