Posts Tagged ‘C#’

The ASP.NET Bin and App_Code folder misconception

Monday, May 5th, 2008 by Agro Rachmatullah

This might very well be my misconception only.

From what I inferred by reading books and articles, when developing an ASP.NET application you have two special folders at your disposal, the Bin and App_Code folder. You can place assemblies in the Bin folder (e.g., MysteryOfLifeSolver.dll) and automagically you can use the classes inside the assembly from your ASP.NET pages. App_Code is for putting C# source code files (e.g., ComplexNumber.cs) and ASP.NET will compile the files for you so that the classes inside can be used on your ASP.NET app.

Except it didn’t work.

Well, since in IIS the directory C:\inetpub\wwwroot corresponds to http://localhost, naturally I put my applications inside there. For example, I might have three ASP.NET applications:

C:\inetput\wwwroot\app1
C:\inetput\wwwroot\app2
C:\inetput\wwwroot\app3

I thought I could easily have a separate Bin and App_Code for each application. For example, for app1 I created:

C:\inetput\wwwroot\app1\Bin
C:\inetput\wwwroot\app1\App_Code

But ASP.NET refused to use those 2 folders! What worked is that I put Bin and App_Code in the wwwroot folder, like:

C:\inetput\wwwroot\Bin
C:\inetput\wwwroot\App_Code

It worked, but obviously created a massive mess because now all my ASP.NET application must share the same Bin and App_Code folder.

The problem is simply because those three applications I made were imaginary. Yes, for IIS/ASP.NET, there was only one ASP.NET application, the one with wwwroot as its root folder. The folder I named app1 etc. was part of that wwwroot application as far as IIS is concerned.

To tell IIS that “Hey! This app1 folder is an application in its own right!”, you need to configure it as a “virtual directory”. Go to the IIS snap in (C:\WINDOWS\system32\inetsrv\iis.msc), keep opening the tree on the left until you meet Default Web Site, right click it, and choose New -> Virtual Directory....

Enter any name for the alias (e.g., newapp), which will determine the URI (e.g., http://localhost/newapp). Next, browse for the directory in the file sytem which contains the files of your application (e.g., C:\inetput\wwwroot\app1). Note that you can even name the alias just like the directory name in the filesystem (e.g., app1 for the previous example). For the permissions, you can leave the default but you might need Write in case your application needs to write into the file system.

After that it’s bliss. You can have Bin and App_Code in each of your application’s root folder.