C# 从 App_code 文件夹中找不到类型或命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9178985/
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-09 06:16:15  来源:igfitidea点击:

Type or namespace could not be found from App_code folder

c#asp.net

提问by johnmcp

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:

我在我的项目的 App_code 文件夹中编写了一个名为 ArchivedFilesWrapper 的类,但是当我在不同文件夹的另一个文件中使用这个类时,我得到错误:

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

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

I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?

我认为每个页面都应该能够找到包含在同一个项目中的类,但我想情况并非如此。有人可以告诉我我需要什么 using 语句吗?

Here is a snippet from my class:

这是我课堂上的一个片段:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EMCWebAdmin.App_Code
{
    public class ArchivedFilesWrapper
    {

采纳答案by johnmcp

If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.

如果您将其转换为网络应用程序,它应该可以工作。缺点是它不再在每次更改和启动应用程序时在 app_code 文件夹中自动构建您的代码。我从未见过专业开发人员使用网站项目。我不知道 MS 在创建它们时针对的是谁。

回答by Ben Robinson

You need to add

你需要添加

using EMCWebAdmin.App_Code;

to all the pages you want to be able to use the class.

到您希望能够使用该类的所有页面。

Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin

或者,您将类所在的命名空间更改为所有网页都使用的命名空间,假设它是 EMCWebAdmin

then in your class change

然后在你的班级改变

namespace EMCWebAdmin.App_Code
    {
...

to

namespace EMCWebAdmin
    {
...

This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.

这是visual studio的一个特性,如果你在文件夹结构中创建一个类,它会使用一个遵循文件夹结构的命名空间。

回答by Bashar Abu Shamaa

Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue. Thanks johnmcp.

是的,将 calsses 放到另一个文件夹(不是 asp.net 特殊文件夹),并且只使用应用程序的主命名空间来解决这个问题。谢谢 johnmcp。

回答by subsci

Perhaps the problem will be solved by changing the Build Action Propertyof the *.cs source file to Compilefrom Content. From the Solution Explorer right click on the source file and choose Property.

也许这个问题可以通过将*.cs 源文件的Build Action 属性更改为Compilefrom Content 来解决。在解决方案资源管理器中右键单击源文件并选择属性。

Note that the App_Codefolder is intended for use in Web Site Projects.

请注意,App_Code文件夹旨在用于网站项目

Note that for a Web Application Projector MVCproject, adding an App_Codefolder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Codefolder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Propertyof the *.cs source file to Compilefrom Contentthe namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.

请注意,对于Web 应用程序项目MVC项目,将App_Code文件夹添加到您的项目并将 *.cs 文件放入其中会导致问题。我无知地从解决方案资源管理器向我的 MVC 项目添加了一个App_Code文件夹。VS 默认命名空间为 MyProjectName.App_Code。在这种情况下,Visual Studio 2012 默认将 Build Action 设为 Content,即使类型是 .cs code。在我将 *.cs 源文件的构建操作属性更改为从内容编译后,命名空间已在项目的其他文件夹位置解析。但是由于问题,我不得不更改文件夹的名称——见下文。

Important

重要的

In the MVCor Web Applicationproject, the App_Codefolder is trouble because it has Web SiteProject type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twiceerrors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Codefolder in the different project types see this blog

MVCWeb Application项目中,App_Code文件夹很麻烦,因为它具有Web SiteProject 类型语义。该文件夹在发布(部署)到服务器时被编译。通过将 Build Action 从 Content 更改为 Compile,您可以通过强制立即编译来解决开发环境中的命名空间问题,但是当第二次编译导致objects defined twice部署错误时,您就会遇到麻烦。将代码文件放在不同名称的文件夹中。如果您将网站转换为 Web 应用程序,请参阅网络上的指南 - 不在此问题的范围内。要阅读有关不同项目类型中App_Code文件夹的更多信息,请参阅此博客