【问题标题】:How to define namespace for imported project如何为导入的项目定义命名空间
【发布时间】:2013-08-21 16:54:54
【问题描述】:

我在尝试引用导入到空白项目中的项目中的方法/类时遇到了困难。重新创建它的确切步骤如下

  1. 下载并解压到一个临时文件夹http://mywsat.codeplex.com/
  2. 在 VS - Asp.Net Empty Web Application .NET 3.5 中创建一个名为 WebApplication1 的新项目
  3. 将 MYWSAT35 文件夹中的所有文件复制到新项目中
  4. 在 VS 解决方案资源管理器中,选择显示所有文件

如果您现在构建并运行项目,则可以正常运行,没有错误。

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 的正确路径修复错误?

【问题讨论】:

  • 原始代码是网站“项目”吗?您的空白项目是 Web 应用程序项目吗?
  • 是的,我想是的。这是否意味着不可能?
  • 这意味着您将苹果和橙子混合在一起,应该会遇到麻烦。我建议将下载的站点下载到本地,右键单击并转换为 Web 应用程序,然后尝试使用该代码。
  • 我确实需要将其转换为应用程序,因为我想将其与现有的 Web 应用程序结合起来。如果您将其添加为答案,我会接受。谢谢。

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


【解决方案1】:

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

【讨论】:

【解决方案2】:

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

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

但是如果你是在VS2012中打开,它会在没有提示的情况下打开它。

两者都会成功构建。

【讨论】:

    【解决方案3】:

    注意:您不要创建新项目。

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

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多