The importance of APP_CODE folder in ASP.NET 2.0 (Beaware of few Facts)

Posted: December 5, 2005 in Uncategorized

IN ASP.NET 2.0 you will find a new folder called APP_CODE .The Purpose of this folder is to share resources thru out the app…That is it can contain source code for utility classes and business objects (.cs..etc)which you want to compile as part of your application.In dynamic compilation upon the first request the codes in this folder are compiled and those get recompiled if any code changes happen. Further you can get strongly typed by placing arbitary type.

 

As I told the code in this folder is accessible thru out to Ur application it gives a certain advantage for code managing.

 

The above features is a part of the New Compilation model in asp.net 2.0 .Compilation of ASP.NET 2.0 no longer produce a Single Assembly (As many Devs provided feedback that a huge assembly is taking quite lot of time for a huge application.).Thus in 2.0 we have now multiple assemblies may be based on the directory structure except the root (Yet needed to be sure ).That is 1 assembly for the Root and subsequent assemblies for the directory struct (one for each).

 

However There are few points needed to taken care before using the App_Code which otherwise create many doubts on your mind (at least I was thinking it’s a missing feature, bydesign or I am wrong for a whileJ).

 

The issue is if you are putting a class (say a1.cs) outside the App_Code and if you are trying to access the same in codebehind file (say webform1.cs) it ll produce the Compilation error as it no longer recognize ur class in CodeBehind.The same is the story the reverse side that is if you are trying to access the CodeBehind or UserControl class inside the Class (say a1.cs ) in App_Code the same ll be the result Compilation Error.

I found that Classes in the Different directory other then App_Code are producing this behavior.

Thus Beware of this if you are migrating any 1.1 apps to 2.0 byhand make sure to place the file appropriately that is place the .cs (utility classes  )in App_code and look at the workaround below:-

 

So now the question arises what is the workaround and why this happens

 

The workaround is simple that is add a reference <%@ Reference … … %> to the reqd classes.However if you don’t have a provision to reference then what you ll do???One solution for the same would be to create a base class put it in App_Code and inherits from it.

 Coming to the design the best Guess for this behavior is seems by design and since all Directory compiled to individual assembly and all have a reference bydefault to the app_code Again providing a Reference from App_Code Classes might causes a circular references.

 

The Questions here are there are problem here when I am developing controls and it can put a certain limitation on me when  i need to ref a class in anotrher directory  .Further I guess if I am putting a class outside the app_code and not in any directory(in root only) then it should be part of the Root assembly but since i am not able access the same class in any codebehind which is in the RootDirectory I need to more digged into the compilation model.

 Update

I am editing this entry as i found quite a few hit on this entry.Thus just a point to mention is that although the above behavior is true we can now have the old behvaior with the new Webproject model shipped in to handle the compilation in alittle different way.For the new compilation check the Fritz onion’s article in msdn and for the new Webproject model check my post here

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s