Saturday, May 10, 2008

Difference between Virtual Directory and an Application in IIS

After a lot of work I finally have the time now to write a useful entry. Almost all the times I have spoken on IIS, I have had an audience who asked me these basic questions.

Q.)What is the difference between Virtual Directory and Virtual Application
Q.)How is ASP and ASP.NET processed within IIS.

Although they are conceptually and functionally different,yet they tend to arise a very reasonable confusion in the mind. So I thought to put the answer down in writing.

Lets take them one by one.

Q.)What is the difference between Virtual Directory and Virtual Application

Before we dig deep into the difference lets go through the definitions:

Virtual Directory is the path that you specify in IIS which maps to the physical location of the content. This path then becomes the part of the URL.

An Application in IIS 6 is a virtual directory which contains some or more of the application pool properties.This again becomes the part of the website URL.

For example, if you have a static file named start.htm which is contained inside a folder named example. When you map example to a website within IIS, it becomes a Virtual Directory. After that, once you associate an application pool to this virtual directory it becomes an Application. But, this was within IIS 6.

With IIS 6 as prospective in mind, the concept is pretty confusing as the difference is not that major. In IIS 6, the basic purpose for this feature was application isolation. Other than this , however, it provided the developers to make complex applications by using the extended feature within ASP and ASP.Net applications.

In IIS 7 however this is pretty different, for the good :)

For IIS 7, Application and Virtual Directory are two different objects and are different parts of the website hierarchy. Within IIS 7, every site must contain an application which will in turn contain one or more virtual directory.

An Application here is a group of files that delivers content or provides services over protocols, such as HTTP.When you create and application it is not only a part of sites but is now also mapped to an Application Pool.

The difference is more apparent when we bring in ASP.Net into the picture, as we will then have to consider the runtime component at the server. At runtime when an ASP.Net application request is received, a new AppDomain is created in the worker process that handles subsequent ASP.NET requests. Therefore, an application can have several virtual directories, and each one will be served by the same AppDomain because of the integrated pipeline structure of IIS 7.

But again you can have a set of Applications and virtual directories with in a single site. Atlast, it only depends on how you plan it out. I would recommend, the static content files to be a part of the virtual directory structure and the dynamic content which may or may not include aspx pages to be part of the application structure within a site.

Q.)How is ASP and ASP.NET processed within IIS.

Well this is a very good question, because the answer of this questions also gives you a very sound surface touch of the IIS processing model.

The description below is how ASP and ASP.Net is processed under IIS6. This process model is now pretty different in the case of IIS 7.

When a request is made by the user for an ASP page, IIS checks the given URL, determines the actual resource name, extension.

Since you have the general extension as .asp for asp web pages IIS maps that request to asp.dll. ASP.dll will process the page and return the HTML equivalent to IIS, which is then sent to the browser.

asp.dll is located at %windir%\system32\inetsrv

Oh BTW, for the beginners, if you come across the word Classic ASP (which is more or less now a standard representation.) it stand for ASP but not ASP.Net. ;)

As for ASP.NET the processing a bit more complex.... you have the general extension as .aspx for asp.net web pages, IIS maps that request to aspnet_isapi.dll. Aspnet_isapi.dll will process the page and return the HTML equivalent to IIS, which is then sent to the browser. However, for the sake of easy understanding, I have not included many features with in ASP.Net processing.

Well I leave it at this, lets see where you would want me to take it. Do put in your comments/questions as always.

10 comments:

Honey said...

Well, can you explain a bit more on difference between Apps/Virtual Directories.

I mean I used to create a VD is IIS 5.1 (Win XP) and was able to run my ASP.NET app. But now I have to create atleast an IIS application to get the same thing running.

ma_khan said...

Honey (Good name by the way),

Major reason for that is because in IIS 5.0 and IIS 5.1 we did not have the concept of AppPools, which has come up in the later versions.

Essentially, now an Application is directly mapped to the AppPool through it's AppDomain. Therefore, your Asp.Net application may contain various Virtual Directories but will run within the same AppDomain.

Honey said...

I checked out my IIS 5.1, and yes it does not have a concept as App Pool.

But I remember, in one of my earlier encounters with a Win Server 2003 (without SP1) system, IIS had the concept of an App Pool. I dont clearly recall, but does that mean that the Server was using IIS 6.0.

I am making some points. Please correct me if I am wrong.

1) You can define multiple Virtual Directories (VDs) for an App Pool.

2) Each VD can have its own web.config file

3) The entire App Pool is recycled if web.config (or Bin Directory) of any VD is modified.

4) All VDs share features like the .NET runtime they are running under. However, other settings we are used to setting differently for each VD (like AuthenticationMode etc.) can still be set differently for each VD in its web.config.

Anonymous said...

I am used to IIS 5.1. I set various settings for an Application in IIS 5.1 either in the IIS MMC or in the web.config for the Virtual Directory.

Can you provide a split of important settings for these in 2 categories for IIS 7:
1) Those defined at App Pool level
2) Those defined at VD level

ma_khan said...

Yes, Windows Server 2003 has IIS 6 and, for the record, Windows Server 2008 and Vista has IIS 7.

Answer 1.) Yes
Answer 2.) You seemed to be confused. Web.config has got nothing to be with VD...It is the configuration file for ASP.NET apps... Check this http://msdn.microsoft.com/en-us/library/aa719558.aspx for more info ...
Answer 3.) Again AppPool recycling has got nothing to do with web.config. Check my blog entry http://www.iisworkstation.com/2008/08/application-pools-in-iis-6.html for more on AppPools.
Answer 4.) Virtual Directory has got nothing to do with the execution your code. Therefore, the question of .Net runtime doesn't even come up. To quote myself "Virtual Directory is the path that you specify in IIS which maps to the physical location of the content. This path then becomes the part of the URL."

I believe I have a good topic to blog about ... keep watchin this place :)

Honey said...

But if I modified web.config for a ASP.NET application, its App Domain used to get recycled.

What I am asking is that if I modify web.config inside one of the VDs for a App Pool, does the entire App Pool or just that VD content gets recycled??

ma_khan said...

Honey,

If you modify an ASP.Net Application. It's corresponding web.config file is changed if needed and depending upon that the AppDomain will be refreshed.

Aman said...

Can you please explain, how to map .html extension with aspnet dll in IIS7.

Anonymous said...

The author's description of the differences between a vdir and an application are a bit confusing, and in some cases misleading. First, do not confuse a web application with an application pool, they are two different things, and exist independently of each other.

An application pool is a collection of settings that define the runtime behavior of the hosting worker process. This is not the same as the settings of a web (or virtual) application running inside that worker process.

For a better description of the difference between a VDir nd application, check out David's description, as I personally find it to be a cleaner and more accurate description:

http://blogs.msdn.com/david.wang/archive/2008/12/07/howto-iis-6-request-processing-basics-part-2-web-site-virtual-directory-and-web-application.aspx

.::Mr beaR::. said...

How do u define an APPS folder in IIS v5.1 ? Hmm....