How flex works with Asp.net

Source: Internet
Author: User
1. Insert the flex compiled program to the Asp.net page.

Flex's final output includes one web page and one flash (.swf file)
You can insert the .swf file into the Asp.net page by using the formula of the other generated web page.

The flex3 project is named testapp. The simplest and most direct method is,
In the "bin-Debug" directory:
Testapp.html
Testapp.swf
Ac_oetags.js
Playerproductinstall.swf
Copy the four files to the bottom of the asp.netwebsite, open testapp.html, and copy the content to the Asp.net program page (. aspx file.
For example, default. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
// Copy all content of testapp.html here
//....
//...

The usage of the hosts file is the same.

There are other requirements: The flex3 program and the web page still have interaction, please use "flex externalinterface" to search

2. Interaction between the flex program and the Asp.net Program

You can use the flex loader to send a request to Asp.net to obtain XML.
You can also use externalinterface to interact with JS on the webpage, so that JS can send Ajax requests to Asp.net.

The following example aims to post data to the Asp.net page on the flex end and display the returned XML data.

// Asp.net code
// Getxml. aspx code. Keep one line and delete other HTML code.
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "getxml. aspx. cs" inherits = "getxml" %>

// Getxml. aspx. CS
// Using system...
Using system. xml;
Public partial class getxml: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String user_pkid = system. Web. httpcontext. Current. Request. Form ["user_pkid"];
If user_pkid! = NULL)
{
Createxml (); // method for creating XML, you can use xmltextwriter, xmldocument, or directly read the XML file and wait
}
}

Private void createxml ()
{
Xmldocument Doc = new xmldocument ();
Xmlnode root = Doc. createelement ("channel ");

Xmlelement titleelm = Doc. createelement ("title ");
Titleelm. innertext = "blogweather ";

//...

Root. appendchild (titleelm );
Doc. appendchild (Root );

Xmltextwriter XW = new xmltextwriter (response. outputstream, system. Text. encoding. utf8); // write it to the return value of the page
XW. Formatting = formatting. indented; // format the XML
Doc. Save (XW );
XW. Flush ();
XW. Close ();
}
}

XML data is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Channel>
<Title> blogweather </title>
<Link> http://www.blogweather.net </link>
<Description> blog weather forecast </description>
</Channel>

Method 1:
If all values are in XML data and do not require secondary analysis, we recommend that you use the httpservice control.

Flex code:

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" initialize = "Init ()">
<Mx: SCRIPT>
<! [CDATA [
Import MX. messaging. abstractconsumer;
Import flash. Events. mouseevent;
Import MX. Controls. Alert;

Private function Init (): void
{
Getxml. url = "http://www.blogweather.net/getxml.aspx"; // page for receiving the POST method
VaR data: Object = new object ();
Data ["user_pkid"] = This. Parameters. user_pkid;
Getxml. Send (data );
}
]>
</MX: SCRIPT>
<Mx: httpservice id = "getxml" showbusycursor = "true" useproxy = "false" method = "Post">
</MX: httpservice>
<Mx: textarea wordwrap = "true" editable = "false" enabled = "true" id = "lb_title">
<Mx: Text> {getxml. lastresult. Channel. Title} </MX: Text>
</MX: textarea>
</MX: Application>

Method 2:
If you want to analyze the data, use urlloader and URLRequest
Flex code:

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" initialize = "Init ();">
<Mx: SCRIPT>
<! [CDATA [
Import MX. messaging. abstractconsumer;
Import MX. messaging. channels. streamingamfchannel;
Import flash. Events. mouseevent;
Import MX. Controls. Alert;

Public var myloader: urlloader = new urlloader ();
Public var myrequest: URLRequest;
Public var user_pkid: string;

Private function Init (): void
{
VaR http://www.cnblogs.com/glaivelee/admin/String = "http://www.blogweather.net/getxml.aspx ";
Myrequest = new URLRequest (URL );
Myrequest. method = urlrequestmethod. post;
VaR data: urlvariables = new urlvariables ();
// Receives parameter calls from flash. For example, if the Flash file is loadxml.swf, what is the parameter loadxml.swf? User_pkid = 10001.
Data. user_pkid = This. Parameters. user_pkid; // obtain 10001
Myrequest. Data = data;
Myloader. Load (myrequest );
Myloader. addeventlistener (event. Complete, onloadcomplete );
}

Private function onloadcomplete (Event: Event): void
{
VaR myxml: XML;
VaR Loader: urlloader = urlloader(event.tar get );
Myxml = New XML (loader. data );

Lb_title.text = myxml. Child ("channel") [0]. Child ("title ");
If (lb_title.text = "blogweather ")
{
Alert ("Page name: blog Weather Report ");
}
}

]>
</MX: SCRIPT>
<Mx: textarea wordwrap = "true" editable = "false" enabled = "true" id = "lb_title">
<Mx: Text> lb_title </MX: Text>
</MX: textarea>
</MX: Application>

Related Article

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.