Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access class from app_code

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.

like image 731
user3532232 Avatar asked Jan 10 '23 22:01

user3532232


2 Answers

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.

like image 107
David Jiboye Avatar answered Jan 12 '23 12:01

David Jiboye


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.

like image 28
Salah Akbari Avatar answered Jan 12 '23 11:01

Salah Akbari