4

I have a web site(not web app) which has a default.aspx and default.aspx.cs. And it has an App_Code folder with class A in it. So default.aspx.cs. Looks like:

namespace test1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

And class A:

namespace test1
{
    public class A
    {
        _Default mDefaultRef;
        public pageLogicIndividual(_Default defaultRef)
        {
            mDefaultRef = defaultRef;
        }
    }
}

I can use A in _Default. eg. if I type test1., while working in class _Default, the IntelliSense will show both classes. But if I type test1., while working in class A, the IntelliSense will only show A. Why can't I use _Default in A?

Error : the type or namespace name does not exist in the namespace (are you missing an assembly reference )

Edit: I'll try to clarify. While working in the .cs file where class _Default resides I can type test1., which is the namespace, and the intellisense will show me class _Default and Class A. But if I'm working in the .cs file where class A resides and type test1., which is the namespace, then the intellisense will only show me class A.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
user3532232
  • 257
  • 8
  • 19
  • "f i type "test1." the IntelliSense will show both classes. But if i type "test1." in A IntelliSense will only show A." bit confusing. What exactly the behaviors is? – Amit Soni Sep 02 '14 at 13:08
  • This is not Classic ASP - please change your tag to [tag:asp.net]. – Paul Sep 02 '14 at 13:11

3 Answers3

11

I have had this challenge in the past.

Open up the App_Code folder node in Visual studio. Right click on the concerned class, then click properties

In the properties pane, change Build Action to Compile.

It should work fine now.

David Jiboye
  • 511
  • 8
  • 19
2

Your problem is with your misleading namespace that you've added yourself after the new file has been created because ASP.NET Web Sites do not have namespace. The namespaces are available in Web Applications projects. i.e. after a new WebSite is created, namespace doesn't added to the files.

So you don't need to place your class A inside the test1 namespace because you can use A in default.aspx.cs even without namespace but you can not access other WebForm page classes from a Webform page or App_Code classes.

BTW if you want to use the necessary and reusable methods within a class of the Default Web Form, you can move those methods out to A class which is under App_Code and as I said already you can use it within all the Web Form CodeFiles without providing namespace for it.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
2

In a nutshell, you cannot access page classes from App_code classes.
This restriction comes from website project compilation model. Special App_code folder contains shared classes (and possibly other resources) which are available to pages (and to each other). During compilation App_code is compiled first in a single assembly. This is the key point. Pages, controls and MasterPages are compiled in another assembly(ies) which may have references to the App_code assembly (but not vise versa). So this is one-way road.
No namespace declaration should circumvent this behavior.
Pages can see each other in ASP namespace (ASP.default_aspx) but pages usually don't have public properties / methods (user controls .ascx usually have).
Read better explanation on MSDN Code-Behind Pages

Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36