C# 驻留在 App_Code 中的类不可访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222281/
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
Classes residing in App_Code is not accessible
提问by Malachi
I have created a website in ASP.NET and have created a class and put it inside of the App_Code folder. However I cannot access this from my other pages. Does something need to be configured to allow this? I have made it work in previous projects, but not in this one, somehow.
我在 ASP.NET 中创建了一个网站,并创建了一个类并将其放在 App_Code 文件夹中。但是我无法从我的其他页面访问它。是否需要配置一些东西来允许这个?我已经让它在以前的项目中工作了,但不知何故在这个项目中没有。
namespace CLIck10.App_Code
{
public static class Glob
{
...
}
}
采纳答案by StevenMcD
Right click on the .cs
file in the App_Code
folder and check its properties.
右键单击.cs
文件App_Code
夹中的文件并检查其属性。
Make sure the "Build Action" is set to "Compile".
确保“构建操作”设置为“编译”。
回答by AaronLS
Put this at the top of the other files where you want to access the class:
将它放在要访问该类的其他文件的顶部:
using CLIck10.App_Code;
OR access the class from other files like this:
或从其他文件访问该类,如下所示:
CLIck10.App_Code.Glob
Not sure if that's your issue or not but if you were new to C# then this is an easy one to get tripped up on.
不确定这是否是您的问题,但如果您是 C# 新手,那么这很容易被绊倒。
Update: I recently found that if I add an App_Code folder to a project, then I must close/reopen Visual Studio for it to properly recognize this "special" folder.
更新:我最近发现,如果将 App_Code 文件夹添加到项目中,则必须关闭/重新打开 Visual Studio 才能正确识别此“特殊”文件夹。
回答by AaronLS
make sure that you are using the same namespace as your pages
确保您使用与页面相同的命名空间
回答by user3363647
Go to the page from where you want to access the App_code class, and then add the namespace of the app_code class. You need to provide a using
statement, as follows:
转到要访问 App_code 类的页面,然后添加 app_code 类的命名空间。您需要提供一份using
声明,如下所示:
using WebApplication3.App_Code;
After that, you will need to go to the app_code class property and set the 'Build Action' to 'Compile'.
之后,您需要转到 app_code 类属性并将“构建操作”设置为“编译”。
回答by Zarepheth
I found it easier to move the files into a separate Class Library project and then reference that project in the web project and apply the namespace in the using
section of the file.
For some reason the other solutions were not working for me, but this work around did.
我发现将文件移动到单独的类库项目中,然后在 Web 项目中引用该项目并using
在文件的部分中应用命名空间更容易。出于某种原因,其他解决方案对我不起作用,但这项工作确实适用。
回答by vapcguy
I haven't figured out yet why this occurs, but I had classes that were in my App_Code
folder that were calling methods in each other, and were fine in doing this when I built a .NET 4.5.2 project, but then I had to revert it to 4.0 as the target server wasn't getting upgraded. That's when I found this problem (after fixing the langversion
in my web.config from 6 to 5... another story)....
我还没有弄清楚为什么会发生这种情况,但是我的App_Code
文件夹中有一些类正在相互调用方法,并且在构建 .NET 4.5.2 项目时可以很好地执行此操作,但是后来我不得不将其恢复为 4.0,因为目标服务器没有升级。那是我发现这个问题的时候(langversion
在我的 web.config 中从 6修复到 5 之后......另一个故事)......
One of my methods kept having an error like:
我的一种方法一直有这样的错误:
The type X.Y conflicts with the imported type X.Y in MyProject.DLL
All of my classes were already set to "Compile" in their properties, as suggested on the accepted answer here, and each had a common namespace that was the same, and each had using MyNamespace;
at the top of each class.
正如此处接受的答案所建议的那样,我的所有类都已在其属性中设置为“编译”,并且每个类都有一个相同的公共命名空间,并且每个类都using MyNamespace;
位于每个类的顶部。
I found that if I just moved the offending classes that had to call methods in each other to another, standard folder named something other than "App_Code", they stopped having this conflict issue.
我发现,如果我只是将必须相互调用方法的有问题的类移动到另一个名为“App_Code”以外的标准文件夹中,它们就不会出现此冲突问题。
Note: If you create a standard folder called "AppCode", move your classes into it, delete the "App_Code" folder, then rename "AppCode" to "App_Code", your problems will return. It doesn't matter if you use the "New Folder" or "Add ASP .NET Folder" option to create "App_Code" - it seems to key in on the name.
注意:如果您创建一个名为“AppCode”的标准文件夹,将您的类移入其中,删除“App_Code”文件夹,然后将“AppCode”重命名为“App_Code”,您的问题将再次出现。如果您使用“新建文件夹”或“添加 ASP .NET 文件夹”选项来创建“App_Code”并不重要 - 它似乎键入了名称。
Maybe this is just a .NET 4.0 (and possibly earlier) issue... I was just fine in 4.5.2 before having to revert!
也许这只是 .NET 4.0(可能更早)的问题......我在 4.5.2 中很好,然后不得不恢复!
回答by hngr18
In my case, I couldn't get a project to build with classes defined in the App_Code folder.
就我而言,我无法使用 App_Code 文件夹中定义的类构建项目。
Can't replicate the scenario precisely to comment, but had to close and re-open visual studio for intellisense to co-operate agree...
无法准确复制场景以进行评论,但必须关闭并重新打开视觉工作室,以便智能感知合作同意...
I noticed that when a class in the App_Code folder is set to 'Compile' instead of 'Content' (right-click it) that the errors were coming from a second version of the class... Look to the left-most of the 3 of the 3 fields between the code pane and the tab. The 'other' one was called something along the lines of 10_App_Code or similar.
我注意到当 App_Code 文件夹中的一个类设置为“编译”而不是“内容”(右键单击它)时,错误来自该类的第二个版本......看最左边的代码窗格和选项卡之间的 3 个字段中的 3 个。'另一个' 被称为 10_App_Code 或类似的东西。
To rectify the issue, I renamed the folder from App_Code to Code, explicitly set namespaces on the classes and set all of the classes to 'Compile'
为了解决这个问题,我将文件夹从 App_Code 重命名为 Code,在类上显式设置命名空间并将所有类设置为“编译”