The Simplest ASP.NET Web Application

Visual Studio is a great IDE but it can be a bit psycho at times. Suppose you want to create the simplest possible ASP.NET Web application. With most .NET project types — a Console Application, a WinForm application, and a Class Library in particular — you can launch an instance of notepad, type in some C# code, and compile from a command line using the csc.exe tool. However, even the simplest ASP.NET Web application is relatively complicated. The crux of the issue is that with Web apps you’ve got to place your C# file(s) on a Web server of some sort. The bottom line is that even though you can create and call a simple ASP.NET Web app without using Visual Studio, in practice you really have to use VS. Here’s how to do the simplest possible Web app. First launch Visual Studio 2008. Now click on File | New | Web Site. This is somewhat misleading terminology because we are about to create a Web application but not a Web site to hold the app. On the New Web Site dialog, select .NET Framework 3.5 in the upper right-hand corner. Next select the Empty Web Site (again the term is somewhat misleading) template. Now in the Location field, you have your choice of File System or HTTP. If you choose File System, after you write your Web application Visual Studio will use a special lightweight Development Web Server. If you choose HTTP, Visual Studio will actually create a fully functional, heavyweight, Internet Information Services (IIS) based Web site on your development machine. Because our goal is simplicity, choose File System. Select C# as the language (VB.NET is a declining language), and then type something like C:\SomeWhere\DescriptiveName in the second part of the Location field. Click OK.
Now in Solution Explorer, right-click on your "DescriptiveName" project name, and select Add | New Item. From the template list, select Web Form (with name Default.aspx). Be sure to uncheck the "Place code in separate file" entry — Visual Studio by default uses a horrible technique of placing code into two separate files, which is not what we want for a simple approach. Click the Add button. In the Visual Studio editor delete the template code in Default.aspx and replace with this:
<%@ Page Language="C#" %>
<script runat="server">
  static int ct = 0;
  protected void Button1_Click(object sender, EventArgs e)
    TextBox1.Text = "Hi!  page load # " + ct;
<head runat="server">
    <title>Simple Page</title>
  <h3>Simple ASP.NET Demo</h3>
    <form id="Form1" runat="server">
     <asp:Button id="Button1" runat="server" Text="Click Me" onclick="Button1_Click"/>
     <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
Now you can exercise the Web application by hitting the F5 key. You will be asked if you want to create a Web.config file to allow debugging — it doesn’t matter what you answer here. Now when the Web app loads into Internet Explorer, you’ll see the URL in the address bar as http://localhost:12345/DescriptiveName/Default.aspx where the port number 12345 has been randomly selected. This is because you are using the special built-in Development Server instead of IIS, and IIS because uses the default port 80, the Development server has to use a different port number. It is possible to specify a hard-coded port number to use. In Solution Explorer, select the DescriptiveName project then do a View | Properties Window. Click into the True value next to the "Use dynamic ports" field, which will enable the Port number field where you can type in some port number. Confusingly, this information gets saved into the Visual Studio .sln file. The location of the .sln file can vary depending upon several factors. A likely location is at C:\Documents and Settings\userXXX\My Documents\Visual Studio 2008\Projects\DescriptiveName. The .sln file has configuration settings including VWDDynamicPort = "false" and VWDPort = 12345. Interestingly, if you simply edit the .sln file by hand, Visual Studio will not read the update until after you close VS, re-launch, and reload the Web application.
This entry was posted in Software Test Automation. Bookmark the permalink.