创建我自己的HtmlHelper扩展时出现问题 [英] Problem creating my own extension to HtmlHelper

查看:82
本文介绍了创建我自己的HtmlHelper扩展时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HtmlHelper的扩展方法:

I have an extension method to HtmlHelper:

<%= Html.MyMethod( params )%>

它可在Visual Studio中工作,但会抛出(在运行时):

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

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

Compiler Error Message: CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for '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?

推荐答案

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

I found the answer in the web.config - there's a section that tells it how to compile C# embedded in 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">
        </compiler>
    </compilers>
</system.codedom>

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

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:

<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>

这篇关于创建我自己的HtmlHelper扩展时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆