C# 找不到命名空间或类(ASP.NET 网站“项目”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1044972/
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
namespace or class could not be found (ASP.NET WebSite "project")
提问by Erick
I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
我目前正在尝试清理我在这里继承的一些公司网站。我设法清除了网站中的大部分错误,但这里仍然存在一些问题。
I have one masterpage that have this code :
我有一个包含此代码的母版页:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
(不,列表中的 S 不是拼写错误)。
Now in App_code I have one class lists.cs
现在在 App_code 我有一个类lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
当我尝试在 Visual Studio 2008 中构建网站时,出现以下错误:
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
我错过了什么吗?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using
" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
我也在服务器上看到了一些奇怪的行为。显然,根据 IIS7,它正在使用 .net 2.0(池配置为 .net 2)"using
进行编译,但有一些" 语句包含 Linq ......如果 Linq 不是 .net 的一部分,如何编译页面2 本身?
Just edited with news details on the code itself. No namespace at all everywhere.
刚刚编辑了代码本身的新闻细节。任何地方都没有命名空间。
采纳答案by Erick
Finally I understood quite lately that it was a websiteand not a web applicationI had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.
最后我最近才明白这是一个网站,而不是一个网络应用程序,我不得不向这里的人提问才能得到它......所以我遇到的所有错误都是很正常的。我还没有机会先转换它。
回答by Rony
include the namespace under which lists calss is defined
包括定义列表 cals 的命名空间
or
或者
define both the master page and lists class under the same namespace
在同一命名空间下定义母版页和列表类
回答by Justin Niessner
Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
确保在您的母版页中,对于列表类所属的命名空间有一个 #include 语句(如果它们位于单独的命名空间中,母版页不会自动选择它)。
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
至于奇怪的服务器端行为,.NET 2.0、3.0 和 3.5 都在 IIS 中的 .NET 2.0 应用程序池中运行。一开始看起来很奇怪,但你习惯了。这是一个更深入解释的链接:
回答by Brian Rasmussen
In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
为了使用类型,您必须引用定义它的程序集并包含适当的命名空间。
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.
仅使用包含命名空间,如果您不使用所述命名空间中的任何类型,则无效。
回答by Youssef
is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace
list.cs 是否包含在命名空间中?如果是这种情况,那么您需要添加命名空间 (yournamespace.lists) 或将其包含在母版页中。另请检查您的 MasterPage 是否在命名空间中
回答by Youssef
Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file's build action was set to “Content” not “Compile” (in the properties window.
你好 - 我有一个类似的问题; 我所有的命名空间和继承都已就位。然后我注意到类文件的构建操作设置为“内容”而不是“编译”(在属性窗口中。
回答by daskd
For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
对于几个月后的答案(可能不是正确的答案)可能有什么价值,我想我应该做出以下贡献:
There is a case that this happens, when you place a web site inside another (ie in a subfolder). In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
当您将一个网站放在另一个网站中(即在子文件夹中)时,会发生这种情况。在这种情况下,唯一合法的 App_Code 文件夹是外部网站的 App_code 文件夹。即主网站根目录下的 App_Code 文件夹。
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.
如果您将类文件放在 ROOT 网站的 App_code 文件夹中,也许(说也许)应该不需要将您的网站转换为 Web 应用程序。
回答by Manu Vishwakarma
Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
将 App_Code 类的 Build Action 修改为 Compile。
清理并构建项目
This worked for me.
这对我有用。
回答by Jett Tripp
I had this problem myself
我自己也有这个问题
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library
它不起作用,因为我引用的项目在其属性中设置为控制台应用程序而不是类库