c# - 找不到扩展方法

标签 c# asp.net iis

我有 ASP.net 网站。在 App_Code 文件夹中,我有一个具有扩展方法的类。从 Visual Studio 运行此网站时,一切正常。但是在我的 IIS 7 服务器上,我收到以下错误:

CS1061: 'System.Data.SqlClient.SqlDataReader' does not contain a definition for 'SafeGetString' and no extension method 'SafeGetString' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly reference?)

但是包含此扩展方法的文件位于 App_Code 文件夹中。我错过了一些重要的事情吗?

扩展方法类:

public static class ExtentionMethods
{
    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        return "NULL VALUE";
    }

    public static int SafeGetInt32(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetInt32(colIndex);
        return -1;
    }

    public static DateTime SafeGetDateTime(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
        {
            try
            {
            }
            catch
            {
                return new DateTime(1800, 1, 1);
            }
        }
        return new DateTime(1800, 1, 1);
    }
}

最佳答案

感谢@AnthonyWJones:How come classes in subfolders in my App_Code folder are not being found correctly?

您需要将 codeSubDirectories 添加到 web.config 中的编译元素

<configuration>
    <system.web>
      <compilation>
         <codeSubDirectories>
           <add directoryName="Extensions"/>
         </codeSubDirectories>
      </compilation>
   </system.web>
</configuration>

关于c# - 找不到扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11550717/

相关文章:

c# - 打印控件

c# - 创建具有值属性的动态 asp.net 复选框

c# - 将文件内容从 php curl(使用 post)发送到 c# webapi

.net - WCF 客户端证书停止工作

c# - 未选中匿名访问时无法访问该网站

c# - 第三方库线程抛出的异常

C# 计算重复数字

c# - 不支持关键字 : Metadata

asp.net - 在 ASP.NET 中的部分回发后连接 JavaScript 处理程序

c# - Windows 2003 群集文件共享上的延迟写入失败