C# 如何解决“找不到类型或命名空间”编译错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/549965/
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
How do I resolve "Type or namespace could not be found" compilation error?
提问by Jesse Hallam
I'm concerned that the web hosting that I'm paying for is not configured correctly. ASP.NET Web Sites which compile and run correctly on my local machine don't work once they're deployed.
我担心我支付的网络托管配置不正确。在我的本地机器上正确编译和运行的 ASP.NET 网站一旦部署就无法工作。
The issue I'm dealing with is a compilation error: CS0246, "Type or namespace could not be found (Are you missing a 'using' directive or assembly reference?)
我正在处理的问题是编译错误:CS0246,“找不到类型或命名空间(您是否缺少 'using' 指令或程序集引用?)
My Web Site contains the following files and folders:
我的网站包含以下文件和文件夹:
websitedemo/Default.aspx
websitedemo/Default.aspx.cs
websitedemo/App_Code/HelloClass.cs
The compilation error occurs if I attempt to use any code contained in my App Code folder. I've included the entire /websitedemo/ folder in this archive if it helps troubleshoot the error: http://kivin.ca/websitedemo/source.zip
如果我尝试使用 App Code 文件夹中包含的任何代码,则会发生编译错误。如果它有助于解决错误,我已将整个 /websitedemo/ 文件夹包含在此存档中:http: //kivin.ca/websitedemo/source.zip
I've left the folder in debug mode. The compilation error screen may be viewed at http://kivin.ca/websitedemo/
我已将文件夹置于调试模式。编译错误屏幕可以在http://kivin.ca/websitedemo/查看
Best regards.
此致。
-- update:
- 更新:
If it helps troubleshoot this error, I've got the behaviour that occurs when I attempt to deploy an ASP.NET WebApplication instead of Website.
如果它有助于解决此错误,那么当我尝试部署 ASP.NET WebApplication 而不是网站时会发生这种行为。
In the case that my aspx file includes a @Page direct like this:
如果我的 aspx 文件包含这样的 @Page 直接:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="myproj._Default" Debug="true" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="myproj._Default" Debug="true" %>
The compilation error I get is: 'Parser Error Message: Could not load type 'myproj._Default'.'
我得到的编译错误是:'解析器错误消息:无法加载类型'myproj._Default'。
The webapps are being deployed via ftp using Build -> Publish -> [X] Replace matching files, [X] Only files needed to run this app. [X] Include App_Data folder.
正在使用构建 -> 发布 -> [X] 替换匹配文件,[X] 仅运行此应用程序所需的文件,通过 ftp 部署 Web 应用程序。[X] 包含 App_Data 文件夹。
采纳答案by Jesse Hallam
I'd like to thank everyone for their support. It's most appreciated. I've been in contact with a specialist at my web hosting company and they've advised me that this isn't a code or server configuration error, but just a missed step in my control panel. Setting my website working directory as a virtual dir resolved the issue.
我要感谢大家的支持。这是最值得赞赏的。我一直在与我的网络托管公司的专家联系,他们告诉我这不是代码或服务器配置错误,而只是我控制面板中遗漏的一步。将我的网站工作目录设置为虚拟目录解决了这个问题。
Thanks to the stack overflow community and the outstanding support people at Softsys Hosting.
感谢堆栈溢出社区和 Softsys Hosting 的杰出支持人员。
回答by Torbj?rn
If you build your site locally and upload the resulting dll (that will contain HelloClass) to the bin directory, I bet it will work.
如果您在本地构建站点并将生成的 dll(将包含 HelloClass)上传到 bin 目录,我敢打赌它会起作用。
回答by vishvananda
Try putting your HelloClass.cs in a namespace and put Using at the top of your default file
尝试将 HelloClass.cs 放在命名空间中,并将 Using 放在默认文件的顶部
回答by Matt
http://msdn.microsoft.com/en-us/library/54dwfbb7.aspx
http://msdn.microsoft.com/en-us/library/54dwfbb7.aspx
i think this may help you... if you have subfolders
我认为这可能对你有帮助...如果你有子文件夹
also, app_code was introduced in framework 2.0, can you check that the web application on the live box is set to use framework 2.0 ? otherwise the classes wont get compiled in there
另外,app_code 是在framework 2.0 中引入的,你能检查一下live box 上的web 应用程序是否设置为使用framework 2.0 吗?否则类不会在那里编译