## Application Control Techniques

ASP provides six different ways to affect the general flow of execution. These six methods are depicted in the following diagrams. The arrows in the diagrams represent the flow of execution.

#### Redirecting

Redirection is the process of diverting a request to another location. Redirecting requires a new request to be sent to the server. However, in general, you should design your application to minimize the amount of communication between the client and server. Many design problems that have been addressed by redirecting in the past can now be accomplished by transferring, which does not initiate a new request. See Coordinating Client/Server Processing for more information on this aspect of application design.

You can accomplish redirecting by using the Response.Redirect method.

#### Transferring

The ability to transfer a request from one ASP page to another was introduced in IIS 5.0. Transferring is similar to redirecting; however, it does not require that a new request be initiated. This is a much more efficient way to control application flow. If you transfer the request to an ASP page outside of the application boundary, the boundary will be temporarily extended to include the external ASP page. The external ASP page will behave as if it were included within the application boundary of the calling ASP page. Therefore, any objects or variables that have been given application scope will still be available in the ASP page to which you have transferred. In addition to being faster than redirecting, transferring preserves all of the ASP built-in objects from the original request, including form values from an HTTP post.

You can accomplish transferring by using the Server.Transfer method.

#### Executing

The ability to execute a particular ASP script and return the result was introduced in IIS 5.0. Executing is similar to a procedure call in most programming languages. This method of application flow control is appropriate if you have developed an ASP application that accomplishes some function that you want to incorporate, but have not built that function into a component.

You can accomplish executing by using the Server.Execute method.

#### Component Invocation

This is probably the most common way to control the flow of an application. The COM programming model is integral to the Windows DNA, and should address the vast majority of design problems. Script Components, a technology supported in IIS 5.1, make it easier to take existing scripts and convert them to components.

You can accomplish component invocation by using the Server.CreateObject method. For more information on script components, see Windows Script Components and Using Components and Objects.

#### Exiting

Under normal circumstances you will want your ASP application to complete each line of script in the page. There may be some circumstances, however, where you will need to simply end the response. For example, if you have detected (by using the Response.IsClientConnected method) that the client is no longer waiting for a response, you will want to terminate the ASP application.

You can accomplish exiting the ASP by using the Response.End method.

#### Procedural Processing

If you want to define subroutines or functions within your .asp file, you can do so by using the procedure syntax that is appropriate for the scripting language you are using. For example, VBScript defines subroutines with the Sub … End Sub syntax and functions with the Function … End Function syntax. JScript, on the other hand, supports procedural processing through function calls. For more information on subroutine processing, see Writing Procedures.

© 1997-2001 Microsoft Corporation. All rights reserved.