C# 如何将在任何文件夹中创建的命名空间访问到C的代码隐藏中

C# 如何将在任何文件夹中创建的命名空间访问到C的代码隐藏中,c#,asp.net,namespaces,three-tier,C#,Asp.net,Namespaces,Three Tier,我在asp.net C中使用NDG VS'10在我的网站中创建了一个文件夹 现在,我在该文件夹中添加了一个名称空间为的类,如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ThreeTierSample.BLL { /// <summary> /// Summary description for Student ///

我在asp.net C中使用NDG VS'10在我的网站中创建了一个文件夹

现在,我在该文件夹中添加了一个名称空间为的类,如下所示:

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

namespace ThreeTierSample.BLL
{
  /// <summary>
  /// Summary description for Student
  /// </summary>
public class Student
{
    public Student()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public void insert()
    { 

    }
  }
但问题是它不允许我添加这个名称空间,即使它没有显示在intellisence中

帮我解决这个问题

下面是我的项目层次结构的屏幕截图:

试试这个:

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

namespace ThreeTierSample.BLL
{
public class Student
{
    public Student()
    {
    } 
    public void insert()
    {     
    }
  }
}

public class TestingClass
{
Student myStudent = new Student();  //  THIS IS OUTSIDE THE ThreeTierNamespace
                                    //  and will work                                      

}

您可能在另一个项目中嵌套了三层名称空间,这就是为什么它没有出现在我们的intellisence中的原因,考虑到您的学生类位于同一解决方案的不同项目中,并且您希望在其他项目中使用该类,比如web项目,您拥有所有UI

因此,我们必须将项目集中在一个解决方案中

Student.BLL 学生网 要解决您的问题,您必须将Student.BLL项目作为参考添加到Student.Web项目中

以下是添加项目作为参考的步骤:

右键单击Student.Web项目 选择添加->引用 将显示一个弹出窗口 在弹出窗口的右面板中,选择解决方案 在中间面板中,将显示项目列表。从中选择Student.BLL并单击确定,如下所述: 现在可以使用名称空间访问类


注意:我在您的快照中观察到,您编写了ThreeTierSamle.BLL而不是ThreeTierSample.BLL。您忘记了“示例”中的“p”

将此类移动到App_Code文件夹中,或在App_Code文件夹下创建新类,并从现有文件复制内容。您还可以在App_Code文件夹下创建文件夹,并添加您需要的类文件工作正常,我知道这个技巧,但我希望folderBLL位于app_code文件夹之外,并希望访问该文件夹内的类,这就是发布此问题的目的。然后创建一个新的类库项目,正如SpiderCode所示,这不是我进一步提到的另一个项目,,BLL是同一个项目的文件夹,所有类都驻留在其中,因此我无法在项目选项卡中获取student.BLL。如果两个类都在同一个项目中,则应该使用名称空间。您能用解决方案资源管理器的图像回答问题吗?我已经发布了快照,请查看。检查我的更新答案。我在答案的末尾加了注释。请检查一下。它会解决你的问题。别忘了投票并把它标记为答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThreeTierSample.BLL;

namespace ThreeTierSample.BLL
{
public class Student
{
    public Student()
    {
    } 
    public void insert()
    {     
    }
  }
}

public class TestingClass
{
Student myStudent = new Student();  //  THIS IS OUTSIDE THE ThreeTierNamespace
                                    //  and will work                                      

}