在 .NET 4.0 MVC3 ASPX 中扩展 HtmlHelper
我的代码可以在 .NET 3.5 应用程序中运行,但当我在 .NET 4.0 中以相同方式设置它时,该代码无法运行。当我尝试扩展 HtmlHelper 时,问题就出现了。在 .NET 4.0 MVC3 中,如果我有一个如下所示的页面:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of MvcApplication2.MyModel))" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%=Html.MyTest%>
</asp:Content>
和一个如下所示的扩展:
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()>
Public Function MyTest(htmlhelper As System.Web.Mvc.HtmlHelper) As String
Return Now.ToLongTimeString
End Function
End Module
它将无法编译,因为“Html”不是 System.Web.Mvc.HtmlHelper 类型与过去一样,它现在的类型为 System.Web.Mvc.HtmlHelper(Of IEnumerable (Of MvcApplication2.MyModel))
。 <%=Html.MyTest%>
上的错误消息是:
'MyTest' is not a member of 'System.Web.Mvc.HtmlHelper(Of System.Collections.Generic.IEnumerable(Of MvcApplication2.MyModel))'.
如何在此配置中扩展 HtmlHelper?我的设置中是否遗漏了某些内容,导致“Html”的类型与过去不同?我在网上没有找到任何有关行为改变的信息,这让我相信我在某个地方做错了事情。
更新:
参考继承自 HtmlHelper 的 HtmlHelper(Of TModel),为什么此代码不起作用?
Partial Public Class HtmlHelper
Public Function MyOtherTest() As String
Return Now.ToLongTimeString
End Function
End Class
引用 <%=Html.MyOtherTest%>
会产生相同的错误消息。
I have code that works in a .NET 3.5 app that isn't working when I set it up the same way in .NET 4.0. The problem comes when I try to extend HtmlHelper. In .NET 4.0 MVC3, if I have a page that looks like this:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of MvcApplication2.MyModel))" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%=Html.MyTest%>
</asp:Content>
and an extension like this:
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()>
Public Function MyTest(htmlhelper As System.Web.Mvc.HtmlHelper) As String
Return Now.ToLongTimeString
End Function
End Module
it won't compile, because "Html" is not of type System.Web.Mvc.HtmlHelper
like it was in the past, it's now of type System.Web.Mvc.HtmlHelper(Of IEnumerable (Of MvcApplication2.MyModel))
. The error message on <%=Html.MyTest%>
is:
'MyTest' is not a member of 'System.Web.Mvc.HtmlHelper(Of System.Collections.Generic.IEnumerable(Of MvcApplication2.MyModel))'.
How would I extend HtmlHelper in this configuration? Am I missing something in my setup that's causing "Html" to be of a different type than it was in the past? I haven't found anything online about a change in behavior, which leads me to believe that I've done something wrong somewhere.
UPDATE:
In reference to HtmlHelper(Of TModel) inheriting from HtmlHelper, why won't this code work?
Partial Public Class HtmlHelper
Public Function MyOtherTest() As String
Return Now.ToLongTimeString
End Function
End Class
Referencing <%=Html.MyOtherTest%>
produces the same error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具有类型化模型的视图会获取类型化 HTML 帮助程序,以便帮助程序方法可以使用模型类型。
但是,
HtmlHelper(Of TModel)
继承了HtmlHelper
,因此您的代码可以正常工作。您可能添加了对
System.Web.WebPages.dll
的引用,因此扩展方法正在扩展System.Web.WebPages.HtmlHelper
,这是一个不同的班级。Views with typed models get typed HTML helpers so that helper methods can use the model type.
However,
HtmlHelper(Of TModel)
inheritsHtmlHelper
, so your code will work fine.It's possible that you've added a reference to
System.Web.WebPages.dll
, so that the extension method is extendingSystem.Web.WebPages.HtmlHelper
, which is a different class.