问题创建我自己的HtmlHelper扩展

时间:2022-11-02 20:52:47

I have an extension method to HtmlHelper:

我有一个HtmlHelper的扩展方法:

<%= Html.MyMethod( params )%>

It works in visual studio, but throws (at runtime):

它在visual studio中工作,但抛出(在运行时):

Compiler Error Message: CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'MyMethod'

编译器错误消息:CS0117:'System.Web.Mvc.HtmlHelper'不包含'MyMethod'的定义

The odd bit is that this does work:

奇怪的是这确实有效:

<%= HtmlHelperExtensions.MyMethod( Html, params ) %>

Why does my method not work as an extension, but does as a normal static call?

为什么我的方法不能作为扩展,但作为普通的静态调用呢?

2 个解决方案

#1


I found the answer in the web.config - there's a section that tells it how to compile C# embedded in HTML:

我在web.config中找到了答案 - 有一节告诉它如何编译嵌入在HTML中的C#:

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" 
                  extension=".cs"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        </compiler>
    </compilers>
</system.codedom>

This is missing an extra flag that tells it to use the 3.5 compiler tricks that let extension methods and anonymous types work in the HTML:

这缺少一个额外的标志,告诉它使用3.5编译器技巧,让扩展方法和匿名类型在HTML中工作:

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" 
                  extension=".cs"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <providerOption name="CompilerVersion" value="v3.5" />
        </compiler>
    </compilers>
</system.codedom>

#2


Make sure you are importing the namespace containing the extension method in your view.

确保在视图中导入包含扩展方法的命名空间。

<%@ Import Namespace="MyProject.MyExtensions"%>

Or add it in your web.config so it will be available on every view:

或者将它添加到web.config中,以便它可以在每个视图中使用:

        <namespaces>
            <add namespace="System.Web.Mvc"/>
            <add namespace="System.Web.Mvc.Ajax"/>
            <add namespace="System.Web.Mvc.Html"/>
            <add namespace="System.Web.Routing"/>
            <add namespace="MyProject.MyExtensions"/>
        </namespaces>

#1


I found the answer in the web.config - there's a section that tells it how to compile C# embedded in HTML:

我在web.config中找到了答案 - 有一节告诉它如何编译嵌入在HTML中的C#:

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" 
                  extension=".cs"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        </compiler>
    </compilers>
</system.codedom>

This is missing an extra flag that tells it to use the 3.5 compiler tricks that let extension methods and anonymous types work in the HTML:

这缺少一个额外的标志,告诉它使用3.5编译器技巧,让扩展方法和匿名类型在HTML中工作:

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" 
                  extension=".cs"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <providerOption name="CompilerVersion" value="v3.5" />
        </compiler>
    </compilers>
</system.codedom>

#2


Make sure you are importing the namespace containing the extension method in your view.

确保在视图中导入包含扩展方法的命名空间。

<%@ Import Namespace="MyProject.MyExtensions"%>

Or add it in your web.config so it will be available on every view:

或者将它添加到web.config中,以便它可以在每个视图中使用:

        <namespaces>
            <add namespace="System.Web.Mvc"/>
            <add namespace="System.Web.Mvc.Ajax"/>
            <add namespace="System.Web.Mvc.Html"/>
            <add namespace="System.Web.Routing"/>
            <add namespace="MyProject.MyExtensions"/>
        </namespaces>