C# 为什么我的 App_Code 文件夹中的子文件夹中的类没有被正确找到?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/155105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:59:28  来源:igfitidea点击:

How come classes in subfolders in my App_Code folder are not being found correctly?

提问by Greg

I am getting the following error when I put class files in subfolders of my App_Code folder:

当我将类文件放在 App_Code 文件夹的子文件夹中时,出现以下错误:

errorCS0246: The type or namespace name 'MyClassName' could not be found (are you missing a using directive or an assembly reference?)

errorCS0246:找不到类型或命名空间名称“MyClassName”(您是否缺少 using 指令或程序集引用?)

This class is not in a namespace at all. Any ideas?

这个类根本不在命名空间中。有任何想法吗?

采纳答案by AnthonyWJones

You need to add codeSubDirectories to your compilation element in web.config

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

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

回答by Marc Gravell

Is it possible that you haven't set the folder as an application in IIS (or your web server)? If not, then the App_Code that gets used is that from the parentfolder (or the next application upwards).

您是否可能没有将文件夹设置为 IIS(或您的 Web 服务器)中的应用程序?如果不是,则使用的 App_Code 来自文件夹(或下一个应用程序向上)。

Ensure that the folder is marked as an application, and uses the correct version of ASP.NET.

确保文件夹被标记为应用程序,并使用正确版本的 ASP.NET。

回答by Mitchel Sellers

As you add folders to your app_code, they are getting separated by different namespaces, if I recall correctly, using the default namespace as the root, then adding for each folder.

当您将文件夹添加到 app_code 时,它​​们会被不同的命名空间分隔,如果我没记错的话,使用默认命名空间作为根,然后为每个文件夹添加。

回答by Firoj Husen Shaikh

Check for BuildAction property of file. This should be set to "Compile"

检查文件的 BuildAction 属性。这应该设置为“编译”

回答by peterincumbria

It might not be the correct way but I find it the easiest.

这可能不是正确的方法,但我认为它是最简单的。

Create the class in the main Folder as usual, then move it with your mouse to your sub-folder. Re-compile and all should be fine.

像往常一样在主文件夹中创建类,然后用鼠标将其移动到子文件夹。重新编译,一切都应该没问题。

回答by TonyH

In Visual Studio (2010 at least, but I recall past versions too), you can right click on the folder, within Solution Explorer, and then choose "Include in Project".

在 Visual Studio(至少 2010,但我也记得过去的版本)中,您可以在解决方案资源管理器中右键单击该文件夹,然后选择“包含在项目中”。

Then on the properties tab for each file (or select them all at once), you choose "Compile" for the "Build Action" property.

然后在每个文件的属性选项卡上(或一次选择它们),为“构建操作”属性选择“编译”。

This worked for me.

这对我有用。