如何为导入的项目定义名称空间

时间:2013-08-19 19:23:31

标签: c# asp.net visual-studio namespaces

我在尝试引用导入到空白项目中的项目中的方法/类时遇到了麻烦。重新创建的具体步骤如下

  1. 下载并解压缩到临时文件夹http://mywsat.codeplex.com/
  2. 在VS中创建一个新项目 - 名为WebApplication1的Asp.Net Empty Web Application .NET 3.5
  3. 将MYWSAT35文件夹中的所有文件复制到新项目
  4. 在VS解决方案资源管理器中,选择“显示所有文件”
  5. 如果你现在构建并运行项目运行正常而没有错误。

    5在VS解决方案资源管理器中,右键单击所有导入的文件,然后选择“包含在项目中”

    现在尝试重建项目我收到错误

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

    GetAdminMasterPage.cs位于WebApplication1\App_Code\class\GetAdminMasterPage.cs,看起来像这样

    #region using references
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Caching;
    using System.Web.UI;
    #endregion
    
    /// <summary>
    /// Gets the MasterPage for the user selected Admin Theme.
    /// </summary>
    public class GetAdminMasterPage : System.Web.UI.Page
    {
        #region Get Admin MasterPage
    
        protected override void OnPreInit(EventArgs e)
        {
            if (Cache["cachedAdminMaster"] == null)
            {
                GetDefaultMasterPage();
            }
            else
            {
                string loadMasterFromCache = Cache["cachedAdminMaster"].ToString();
                Page.MasterPageFile = loadMasterFromCache;
            }
        }
    
        private void GetDefaultMasterPage()
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("sp_admin_SelectMasterPage", con);
                cmd.CommandType = CommandType.StoredProcedure;
    
                con.Open();
    
                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.SingleResult);
    
                if (myReader.Read())
                {
                    string masterPageFileName = myReader["ThemeUrl"] as string;
                    Cache.Insert("cachedAdminMaster", masterPageFileName, null, DateTime.Now.AddSeconds(300), System.Web.Caching.Cache.NoSlidingExpiration);
                    Page.MasterPageFile = masterPageFileName;
                }
    
                myReader.Close();
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    
        #endregion
    }
    

    现在给出错误的方法之一是

    using System;
    
    public partial class admin_admin_edit_css : GetAdminMasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    
    //gives error The type or namespace name 'GetAdminMasterPage' could not be found 
    (are you missing a using directive or an assembly reference?)
    

    所以我知道我必须用Using xxxxxx;引用GetAdminMasterPage,但是无法找出正确的语法。

    首先,为什么只有在我选择“包含在项目中”而不是刚刚复制时才会出现错误?

    其次,如何使用GetAdminMasterPage的正确路径修复错误?

3 个答案:

答案 0 :(得分:2)

注意:您不创建新项目。

1.复制驱动器中的MYWSAT35文件夹(例如:d:\ MYWSAT35)
2.在Visual Studio中转到文件 - &gt;打开网站
3.选择d:\ MYWSAT35并单击打开
4.按F5键运行应用程序

答案 1 :(得分:2)

您必须将该项目作为网站打开。

如果您使用的是VS2010,则会提示您升级到.net 4.0。你可以选择不。

但如果您在VS2012中打开,它将在没有提示的情况下打开它。

两者都将成功构建。

答案 2 :(得分:2)

将项目作为网站打开后,右键单击该项目并选择“转换为Web应用程序”。转换的结果是您要移动到空白项目的结果,或者您可能希望保留更改。

相关问题