다음을 통해 공유


A Client Using ASP.NET

One of the most important features of .NET is the ability to execute the same code in ASP.NET as in stand-alone client applications. Of course, there are a few differences. The ASP.NET page generally produces HTML code in response to an HTTP request, and the page itself is compiled dynamically, unlike what was done for the previous examples. Each ASP.NET page is individually parsed, and the syntax is checked. Finally, a .NET runtime class is produced, which is compiled and invoked. ASP.NET caches the compiled object, so subsequent requests do not go through the compile step and thus execute much faster.

Most existing ASP pages use Microsoft JScript or Microsoft Visual Basic Scripting Edition (VBScript). A .NET-compatible version of JScript is provided with the .NET Framework, and VBScript code upgrades quite naturally to Visual Basic. The following code shows the page using Visual C#, but the same page could just as easily have been written in Visual Basic or JScript:

Listing 11. Client in ASP.NET (ClientASP.aspx)

<%@ Page Language="C#" Description="ASP.NET Component Test" %>
<%@ Import Namespace="CompCS"%>
<%@ Import Namespace="CompVC"%>
<%@ Import Namespace="CompVB"%>

<html>
<script language="C#" runat=server>
void Page_Load(Object sender, EventArgs EvArgs) {
   String Out = "";
   Int32 Count = 0;

   // Iterate through the component's strings, and concatenate them.
   Out = Out + "Strings from C# StringComponent<br>";
   CompCS.StringComponent myCSStringComp = new 
   CompCS.StringComponent();
   for (int index = 0; index < myCSStringComp.Count; index++) {
     Out = Out + myCSStringComp.GetString(index) + "<br>";
   }
   Out = Out + "<br>";

   // Iterate throught component's strings, and concatenate them.
   Out = Out + "Strings from Visual C++ StringComponent<br>";
   CompVC.StringComponent myVCStringComp = new 
   CompVC.StringComponent();
   for (int index = 0; index < myVCStringComp.Count; index++) {
      Out = Out + myVCStringComp.GetString(index) + "<br>";
   }
   Out = Out + "<br>";

   // Iterate over component's strings and concatenate.
   Out = Out + "Strings from Visual Basic StringComponent<br>";
   CompVB.StringComponent myVBStringComp = new 
   CompVB.StringComponent();
   for (int index = 0; index < myVBStringComp.Count; index++) {
      Out = Out + myVBStringComp.GetString(index) + "<br>";
   }

   Message.InnerHtml = Out;
}
</script>
<body>
   <span id="Message" runat=server/>
</body>
</html>

This is essentially the same code as that in the stand-alone client examples, except that it creates a string (named out), and assigns the string to a property of an HTML server control.

**Note   **The code could have used the familiar Response.Write to write the string directly into the HTML output stream.

The page specifies Visual C# as a language, but it could just as easily have used Visual Basic or even JScript:

<%@ Page Language="C#" Description="ASP.NET Component Test"

Importing libraries in ASP.NET is also a little different:

<%@ Import Namespace="CompVB"%>
<%@ Import Namespace="CompCS"%>
<%@ Import Namespace="CompVC"%>

The previous lines use <%...%> to indicate script code, and they specify the namespaces to import. As mentioned earlier, the assemblies must be located in the \Bin subdirectory of the application's starting point.

Another subtlety of this page is this line:

<script language="C#" runat="server">

This tells the server to execute the code on the server rather than sending the code text back to the client as part of the HTML stream.

Web Forms provides special recognition of the following six methods:

  • Page_Init
  • Page_Load
  • Page_DataBind
  • Page_PreRender
  • Page_Dispose
  • Page_Error

These methods are automatically connected to event handlers for the standard page events. The most-commonly used event handler is that for Load, and it contains most of the code for the sample program:

void Page_Load(Object sender, EventArgs EvArgs) {...}

The rest of the code is pretty straightforward. It just concatenates strings into a longer string named out, and adds out to the HTML code with this statement:

Message.innerHTML = Out

Testing this page requires a few steps. First, it is necessary for the test computer to have the following pieces of software installed:

  • Internet Information Services (IIS).
  • The common language runtime.
  • ClientASP.aspx.
  • The CompVC.dll, CompCS.dll, and CompVB.dll compiled components.

Installing the .NET Framework SDK on a computer that already has IIS installed allows that computer to run ASP.NET. (If you install IIS after installing the SDK, you should reinstall the SDK.)

Second, you must configure a virtual directory (using Internet Services Manager) that points to the directory where ClientASP.aspx is located. To create a virtual directory using the IIS snap-in, perform the following steps:

  1. Select the Web site or FTP site that you want to add a directory to.
  2. Click the Action button, point to New, and select Virtual Directory.
  3. Use the New Virtual Directory wizard to complete the task.

If you are using the NTFS file system, you can also create a virtual directory by right-clicking a directory in Windows Explorer, clicking Sharing, and clicking the Web Sharing tab. For more information, see the topic "Creating Virtual Directories" in the IIS documentation, which is located at https://localhost/iisHelp/ on the computer that IIS is installed on.

Alternatively, the virtual directory can be created by running the CreateVRoot.vbs VBScript file, which is located in the CompTest directory.

Third, the compiled-component DLLs should be located in the \Bin subdirectory under the starting point for the application virtual directory.

Assuming that everything is configured correctly, executing the file with the https://localhost/NetSDK/PlatformIntroduction/ClientASP.aspx URL results in an instance of Internet Explorer that has a display similar to this:

See Also

Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces