简体   繁体   English

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

[英]How to define namespace for imported project

I'm getting really stuck trying to refer to methods/classes in a project imported into a blank project. 我真的很难尝试引用导入到空白项目的项目中的方法/类。 The exact steps to recreate this are as follows 重新创建此文件的确切步骤如下

  1. Download and Extract into a temp folder http://mywsat.codeplex.com/ 下载并解压缩到临时文件夹http://mywsat.codeplex.com/
  2. Create a new project in VS - Asp.Net Empty Web Application .NET 3.5 named WebApplication1 在VS中创建一个新项目-Asp.Net空Web应用程序.NET 3.5,名为WebApplication1
  3. Copy all files from MYWSAT35 folder across to the new project 将所有文件从MYWSAT35文件夹复制到新项目
  4. In VS solution explorer, select Show all files 在VS解决方案资源管理器中,选择“显示所有文件”

If you now build and run the project runs fine with no errors. 如果现在构建并运行该项目,则运行正常,没有错误。

5 In VS solution explorer, for all of the imported files right click and select Include in Project 5在VS解决方案资源管理器中,对于所有导入的文件,右键单击并选择“包括在项目中”

Now try rebuilding the project I get the error 现在尝试重建项目我得到错误

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

GetAdminMasterPage.cs is located in WebApplication1\\App_Code\\class\\GetAdminMasterPage.cs and looks like this 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
}

An example of one of the methods that now gives an error is 现在给出错误的一种方法的示例是

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?)

So I know I must refer to GetAdminMasterPage with Using xxxxxx; 所以我知道我必须Using xxxxxx;来引用GetAdminMasterPage Using xxxxxx; but just can't figure out the correct syntax. 但只是找不到正确的语法。

Firstly why does the error only occur when I select "Include In Project" and not when just copied? 首先,为什么仅当我选择“包含在项目中”而不是仅在复制时才发生错误?

Secondly, how do I fix the error with the correct path to the GetAdminMasterPage? 其次,如何使用正确的GetAdminMasterPage路径修复错误?

Note: you do not create a new project. 注意:您不创建新项目。

1.Copy the MYWSAT35 folder in a drive (for example: d:\\MYWSAT35) 1.将MYWSAT35文件夹复制到驱动器中(例如:d:\\ MYWSAT35)
2.in Visual Studio go to File -> Open web site 2.在Visual Studio中转到文件->打开网站
3.select d:\\MYWSAT35 and click on open 3.选择d:\\ MYWSAT35并单击打开
4.press F5 key to run application 4.按F5键运行应用程序

You have to open the project as a web site. 您必须作为网站打开该项目。

If you are using VS2010, then it will prompt you for upgrade to .net 4.0. 如果您使用的是VS2010,它将提示您升级到.net 4.0。 You can choose no. 您可以选择不。

But if you are opening in VS2012, it will open it with no prompts. 但是,如果您在VS2012中打开,它将在没有提示的情况下将其打开。

Both will build successfully. 两者都将成功构建。

After opening the project as a web site, right-click the project and choose "Convert to Web Application". 在将项目作为网站打开后,右键单击该项目,然后选择“转换为Web应用程序”。 The result of the conversion is what you'll want to move to your blank project, or perhaps you'll want to leave the changes in place. 转换的结果是您想要移至空白项目的内容,或者您​​可能希望将更改保留在原处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM