Iis – How to setup an App_Code virtual directory in IIS7

configurationiisiis-6iis-7

In IIS6 you can't setup the App_Code directory as a virtual directory.

However, I found that you can setup a virtual directory within the App_Code directory and IIS6 will compile the code into the __code assembly.

It seems that IIS7 won't do this, but I wonder if there is a different way to share a code directory across multiple sites in IIS7.

One option that does work is to create a Junction to the common source code directory, but I really enjoyed having the virtual directory option and by definition the code automatically inaccessible via FTP.

Best Answer

Recommended? probably not. Illegal? maybe. Possible? yes.

You can use virtual directories in the App_Code directory of an ASP.Net file-based website hosted in IIS7.

Create the empty path on disk

First, create a file structure similar to the path below, where "MyWebRoot" is the root directory of your website and "MySharedCode" is the name you want for the virtual directory that will contain the shared *.cs or *.vb source code files.

E:\MyWebRoot\App_Code\MySharedCode

Mask the physical path with the virtual directory

Next, open the Internet Information Services (IIS) Manager and find your site under the "Sites" node of the server.

Click the plus next to the site name to expand the site. You will see the top level directories listed.

Right-click on the "App_Code" directory and choose "Add Virtual Directory..." (See screenshot)

IIS Virtual Directory Dialog Screenshot

Type the name of your shared code virtual directory into the "Alias" text box. This name must match the name of the empty folder you created in the App_Code directory.

Finally, enter the actual path to the shared code location in the "Physical path" text box.

Click "OK" to finish creating the virtual directory.

The next time you browse the site, IIS should compile the files located in the physical path that your virtual directory references.

NOTE: You may need to reset IIS or "touch" the Web.Config file to kickstart that process.

Start > Run > iisreset

Apparently, IIS gives precedence to the content of the virtual directory but it needs the presence of the physical path to motivate it to compile.