Recipe 20.9 Calling ASP.NET Functions from Flash

20.9.1 Problem

You want to call ASP.NET service functions using Flash Remoting.

20.9.2 Solution

Use ASP.NET pages (which requires modifying the code) or, preferably, use public DLL methods (which requires no modification).

20.9.3 Discussion

You can call ASP.NET pages or public DLL methods as service functions from a Flash movie using Flash Remoting.

Ideally, DLL methods should be used in place of ASP.NET pages when it comes to Flash Remoting. Although some people may have hesitancy about writing/using DLLs because they require compilation, they are far more advantageous than using ASP.NET pages. Any public method of an ASP.NET DLL can be called using Flash Remoting without any modification to the DLL code. Furthermore, using DLLs allows you to separate your business logic from your presentation logic, so the same DLL can be used by multiple interfaces (Flash movies, ASP.NET pages, Windows Forms, etc.). Using DLLs for your business logic is a best practice regardless of whether you are using Flash Remoting.

To call a DLL method from a Flash movie, you should place that DLL in the web application's bin directory and create a service object that maps to the class within it. The correct service name to use is the fully qualified class name, which must include the namespace. For example:

// Create a service object that maps to the Book class in the OReilly.ASCB namespace.
myService = myConnection.getService("OReilly.ASCB.Book");

// Call the getTitle(  ) method of the Book class and specify a response object, as
// shown in Recipe 20.7, to handle the results.

If you must use an ASP.NET page, there are several modifications you must make to access it via Flash Remoting.

You must create a FlashGateway.Flash object in the page. By convention, this object is named flash. You can accomplish this one of two ways:


Use the Register directive to register a tag prefix to the FlashGateway namespace and the flashgateway assembly. Then create an instance of the Flash class with a <Macromedia:Flash> tag. The ID attribute should be the name of the object, and the runat attribute should always be "server". For example:

<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash ID="flash" runat="server" />
Code block

Whether in a render block in the ASP.NET page or in a code-behind class, you can create an instance of the FlashGateway.Flash class using the constructor. The following example is in C#:

FlashGateway.Flash flash = new FlashGateway.Flash(  );

If you want to accept parameters, you must extract them from the FlashGateway.Flash object's Params property. Params implements IList, and the parameters are stored as elements with integer indexes.

string myFirstParam  = flash.Params[0];
string mySecondParam = flash.Params[1];

If you want to return a value to the Flash movie, you must assign that value to the FlashGateway.Flash object's Result property. The standard output of the ASP.NET page is disregarded by Flash Remoting. Only the value of the Result property is returned to Flash.

When you want to call an ASP.NET page as a service function, the service name is the path to that page within the web application's hierarchy. Slashes should be replaced by dots. For example:

// Create a service object that maps to the /aspx/users directory 
// of the web application.
myService = myConnection.getService("aspx.users");

// Create a service object that maps to the root directory of the web application.
myService = myConnection.getService("");

The service function name should be the same as the filename of the ASP.NET page. For example:

// Invoke the page called getUserInfo.aspx.

    Part I: Local Recipes
    Part II: Remote Recipes