Flashremoting practice (2)-hello world for. net

Source: Internet
Author: User
Next we will teach you to write the simplest hello world in the. net environment. Now let's talk about it in a few minutes:

Here I suppose you have read my first article on configuration and successfully executed the first two examples.

1. first, go to your virtual directory c: \ Inetpub \ wwwroot. here you will see the flashremoting folder, which is the folder of the example file. Create a New myASPApp folder in this directory, here, the program we developed is put under this folder. Here we create a directory named bin. After the program is created, return to c: \ Inetpub \ wwwroot, Go To The flashremotingdirectory, copy the flashgateway.dlland frconfig.txt files in the directory to the bin directory under myASPApp, and then copy the gateway under flashremoting. aspx and web. the two config files are copied to the myASPApp directory. Here I will not specifically explain why we need to copy these files to the corresponding directory, if you want to know, see the built-in Help File remoting supports four types. net technology:
Asp.net
Ado.net data-binding
Web services
Assembly (with the. dll extension)
To allow the remoting program we write to find the asp.net page, you must put the asp.net page in a virtual directory or subdirectory, that is to say, we have created a myASPApp folder under the virtual directory to differentiate the projects we want to develop. We can put the asp.net folder we need here. Now, let's start writing programs.

2. Create a. aspx file under myASPApp, that is, the asp.net file named helloWorldNET. aspx.
First, we need to declare the language you are using. Here we use vb.net to write

<% @ Page language = "vb" debug = "true" %>

To call data from a flash application or return the result to flash on the asp.net page, you need to use flash remoting on the asp.net page to customize the server side to control this control again. dll, that is, copy to the flashgateway in the bin directory. dll, you must register this control before the program code, the method is this code
<% @ Register TagPrefix = "Macromedia" Namespace = "FlashGateway" Assembly = "flashgateway" %>

This registration explains how to create an identifier prefix Macromedia, register it with the namespace FlashGateway, and provide the function of the dynamic connection library file flashgateway. After registration, you can use it to pass data to the flash application, for example
<Macromedia: Flash ID = "Flash" runat = "server">
Hello from. NET!
</Macromedia: Flash>

I personally think that any code written between <Macromedia: Flash ID = "Flash" runat = "server"> </Macromedia: Flash> will be passed to flash as a return value, compared.. net. wirte () and System in java. out. print (); when it comes to this, everyone may have written this program. Good news, the above Code is the hello world Program, but here I do not advocate this writing, there is another way to write it, after registration, directly write the code <Macromedia: Flash ID = "Flash" runat = "server"/>, and then use asp.net's normal syntax below.

<Script runat = "server">
Sub page_load () sub page_load (sender as object, e as eventargs)
Flash. result = "hello world !!! "
End sub
</Script>

Here we use page_load, which is the Program executed during page loading. In this way, when this page is loaded, hello world will be passed directly to the result method of the flash Object, this is the method for receiving values in flash. After the asp.net page has been written, let's take a look at the complete code.

<% @ Page language = "vb" debug = "true" %>
<% @ Register TagPrefix = "Macromedia" Namespace = "FlashGateway" Assembly = "flashgateway" %>
<Macromedia: Flash ID = "Flash" runat = "server"/>
<Script runat = "server">
Sub page_load () sub page_load (sender as object, e as eventargs)
Flash. result = "hello world !!! "
End sub
</Script>

3. the following is the flash code we want to write. First, we need to import the remoting class to flash. The method is very simple: select the menu bar, window -- other panel -- public database -- remoting, at this time, you will see the remoting database panel on the right of the interface, drag the RemotingClasses to the scene to delete it, so that the RemotingClasses class will be added to the library of our files, when the program is executed, it is imported into the program. If you want to debug the program in the NetConnection Debugger panel, you need to drag the RemotingDebugClasses in the remoting class library to the scenario, now, write the as program.

4. Drag a textinput component to the home scene and name it messageDisplay_txt. Then select the first option in the main scenario to open the action panel. First, you need to import the required class Code as follows:

Import mx. remoting. Service;
Import mx. rpc. RelayResponder;
Import mx. rpc. FaultEvent;
Import mx. rpc. ResultEvent;
Import mx. remoting. PendingCall;

Then, the first step is to connect to the server, so the code for creating a service object is as follows:

Var howdyService: Service = new Service ("http: // localhost/myASPApp/gateway. aspx ",
Null, "myASPApp", null, null );

The first parameter is http: // localhost/myASPApp/gateway. a... pp. helloworld.

Step 2: Call the helloworld method on the server

Var pc: PendingCall = howdyService. helloWorldNET ();

Here you will see that the method of the call server is actually the name of helloWorldNET. aspx on the asp.net page.

Step 3: Write the results returned by the server after the call method and the method of failure.

Pc. responder = new RelayResponder (this, "serviceFunctionName_Result", "serviceFunctionName_Fault ");

If the call succeeds, execute our custom serviceFunctionName_Result method. If the call fails, execute the custom serviceFunctionName_Fault method.
Then we need to write the custom two methods. The first is the serviceFunctionName_Result method. The Code is as follows:

Function serviceFunctionName_Result () function serviceFunctionName_Result (result: ResultEvent ){
// Display successful result
MessageDisplay_txt.text = result. result;
}

Here, after the call is successful, the server result will be passed to the result object. The result method of the result object is the value obtained from the server, and we will put it in messageDisplay_txt to display it.
When a call fails, flash will call the serviceFunctionName_Fault method. The Code is as follows:

Function serviceFunctionName_Fault () function serviceFunctionName_Fault (fault: FaultEvent ){
// Display fault returned from service
MessageDisplay_txt.text = fault. fault. faultstring;
}

The failure information will be passed to the fault object and displayed with messageDisplay_txt. After the program is completed, the complete code is as follows:

Import mx. remoting. Service;
Import mx. rpc. RelayResponder;
Import mx. rpc. FaultEvent;
Import mx. rpc. ResultEvent;
Import mx. remoting. PendingCall;
// Connect to service and create service object
Var howdyService: Service = new Service ("http: // localhost/myASPApp/gateway. aspx ",
Null, "myASPApp", null, null );
// Call the service helloWorld () method
Var pc: PendingCall = howdyService. helloWorldNET ();
// Tell the service what methods handle result and fault conditions
Pc. responder = new RelayResponder (this, "serviceFunctionName_Result", "serviceFunctionName_Fault ");
Function serviceFunctionName_Result () function serviceFunctionName_Result (result: ResultEvent ){
// Display successful result
MessageDisplay. text = result. result;
}
Function serviceFunctionName_Fault () function serviceFunctionName_Fault (fault: FaultEvent ){
// Display fault returned from service
MessageDisplay. text = fault. fault. faultstring;
}

After reading it, do you think it is called. net method is actually very simple, good, as the Code is actually relatively fixed, as long as you find the correct gateway. aspx file boot, and then find the correct asp.net page you wrote, there should be no problem. Here, I pay attention to the as2.0 Writing Method and do not explain every object such as Service or PendingCall in detail. If you want to know these objects carefully, please refer to the relevant materials.

The next section will introduce the hello world Program in the java environment. I will try my best to write it as quickly as possible. Due to the rush of time, it is inevitable that something wrong is written, if wrong, please correct me, if you want to discuss remoting with me, please contact me, My QQ: 22339146, msn: lwanchen@hotmail.com.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.