Creating an ASP.NET Web Application from Scratch

I was teaching an introduction to ASP.NET class last week and a very confusing idea for beginners is Web application creation. If you create a Web app using Visual Studio, all kinds of magic happens behind the scenes which makes it difficult if you want to know how to create a Web app from scratch. In this blog entry I’ll try to explain. If you use Visual Studio 2005 on Windows Server 2003 (unfortunately the details are somewhat different on Windows XP and I won’t cover XP here) to create a new Web Application, you can go File | New | Web Site. Right away this is confusing. You are not really creating a Web site, you are creating a Web application on your existing Web site. This is just plain bad wording on the part of VS. In the Location field, suppose you specify http://localhost /SomePlace  /AppCreatedByVS and then click OK as shown in the first image below. To see the result, you can go to the IIS Manager (right-click on My Computer | Manage | Services and Applications). As shown in the second image below, you get an ordinary directory named SomePlace which contains a special directory named AppCreatedByVS that has a gear icon, which means it holds an ASP.NET application. Notice that Visual Studio automatically creates two files: Default.apx which holds the HTML and display code, and a separate Default.aspx.cs which holds the C# logic code. Now if you examine your file structure using Windows Explorer as shown in the third image, you’ll see that Visual Studio magic actually created a physical organization of C:\Inetpub\ wwwroot\ SomePlace\ AppCreatedByVS that maps to your virtual structure. (The screenshots were taken on a machine where the D: drive is the system drive rather than the C: drive.) The Web application is accessed in Internet Explorer by navigating to http://localhost/ SomePlace/ AppCreatedByVS/ Default.aspx (where you can leave off the "Default.aspx" if you wish).
OK, that’s all just fine, but what if you want to create an ASP.NET Web application without using Visual Studio? Let’s see how to create a Web app from scratch that you’d access by navigating to http://localhost/ OtherPlace/ AppCreatedByIIS/ Default.aspx. First, you must manually set up a directory structure except for the final directory. So, in Windows Explorer you must create a directory named OtherPlace at C:\Inetpub\ wwwroot\. (In advanced scenarios you can place the directory somewhere else.) See the fourth image below. Now in the IIS Manager, select directory OtherPlace and then right-click select New | Virtual Directory. (Important side note: if you have the Default Web Site entry selected then you’ll get an option to create a New Web Site. This is not what you want in spite of the fact that Visual Studio incorrectly calls a new Web application a new Web Site.) The wizard will ask you for the Alias; here you enter the name for the last-level directory, which is also the name of the Web application, which in this case would be AppCreatedByIIS. Next the wizard asks you for the physical directory which will hold your files. This part is very tricky — you use the Browse dialog to go to directory OtherPlace (in other words to one directory higher than the ultimate application directory), then click on the Make New Folder button, which makes a directory New Folder, which you can then right-click on and select Rename, and then rename to AppCreatedByIIS (in other words you give the directory the same name as the Virtual Directory alias), and then select this new directory. See the fifth image below. Note: this ballet is the only sequence that gives you the exact same result as Visual Studio, but there are many different orders of steps you can use that will give you similar, but not exactly the same, results. Next you’ll see Permissions. You must check Run Scripts as shown in the sixth image below. After finishing up, the seventh image shows you the result as you’d see in the IIS Manager. Notice there are no files in directory AppCreatedByIIS. Here is where you could create a single Default.aspx file which contains both HTML display and C# logic code (or separate files for display and logic). However, there is yet another detail. The last image below shows you that when you create a Web application from scratch, the Application Pool setting is "DefaultAppPool" rather than "ASP.NET V2.0" as it is when you create a Web app using Visual Studio. Additionally, you should check the ASP.NET tab (in the upper right hand corner) to make sure you are set to ASP.NET 2.0 version.
This entry was posted in Software Test Automation. Bookmark the permalink.