简体   繁体   中英

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/
  2. Create a new project in VS - Asp.Net Empty Web Application .NET 3.5 named WebApplication1
  3. Copy all files from MYWSAT35 folder across to the new project
  4. In VS solution explorer, select Show all files

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

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

#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; 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?

1.Copy the MYWSAT35 folder in a drive (for example: d:\\MYWSAT35)
2.in Visual Studio go to File -> Open web site
3.select d:\\MYWSAT35 and click on open
4.press F5 key to run application

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. You can choose no.

But if you are opening in VS2012, it will open it with no prompts.

Both will build successfully.

After opening the project as a web site, right-click the project and choose "Convert to Web Application". 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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