View Full Version : varibles/functions between ASP.NET/Flash
strictDesign
10-19-2008, 07:55 PM
i had just started to learn ASP.NET like a month or 2 ago and i had been wondering
becouse u CANT (or i just dont know) send AS3.0 to C# function how u do it ?
the only thing which i thought about was sending to another page with navigate to url and add query like "somehtml.aspx?bla=true"
tough that can work , how i do it that i want flash to stay in the same page and not to go to other page , or i just want it to be more secure..
just as example , it wont work if it would be a score point and then someone who knows bit asp.net or php would go "somehtml.aspx?score=99999999999"
yea , it wont be really healthy for me ^_^;;
zompo
10-19-2008, 08:15 PM
You can always sniff the traffic flash is sending, so even if you have a page like XMAMXSAKDAKSLDAK.aspx its easy to sniff the package sent to your server from the flash and change it to something else.
yell0wdart
10-20-2008, 02:23 AM
Well, you would send the request to the aspx page just like you would a php page. In AS3, you could use the URLLoader/URLRequest/URLVariables classes to send the request. It wouldn't necessarily navigate away from the page...
var variables:URLVariables = new URLVariables();
var sender:URLRequest = new URLRequest("mypage.aspx");
var loader:URLLoader = new URLLoader;
sender.method = URLRequestMethod.POST;
sender.data = variables;
variables.personID = "1234";
variables.name = "Joe";
loader.load(varSend);
That should send your request asynchronously to the aspx page through the url query string (ie: myPage.aspx?personID=1234&name=Joe). In your C#, you can just access the information passed through...
protected void Page_Load(object sender, EventArgs e)
{
int personID = int.Parse(Reqeust.QueryString["personID"]);
string personName = Request.QueryString["name"];
}
just as example , it wont work if it would be a score point and then someone who knows bit asp.net or php would go "somehtml.aspx?score=99999999999"
yea , it wont be really healthy for me ^_^;;
Since you're sending the request behind the scenes (asynchronously), you could use SSL to protect the information if it needs to be a bit more secure. You can also validate the request to ensure that it's only acted upon if it's sent from the page that your swf file is embedded in.
fx.barrett
11-06-2008, 01:58 AM
Thanks yellow for this example, I have just started reading up more seriously on ASP.NET in C# and this example is just what I've been looking for. Thanks. ;)
EDIT: and how would you send a message from ASP.NET back to Flash ? Is it possible to send an Array of information or maybe format some code that we've read from the database to XML and send that XML object back to Flash ( it obviously is possible but could you throw together a fast example ? ).
yell0wdart
11-06-2008, 04:07 PM
Well, there are a few different ways you can do it. One of the easiest ways would be to write a web service in C# and call it from ActionScript. That way you don't even have to worry about messing with http requests. You could just use a WebService object in ActionScript to load the WSDL url, set up the event listener and call the web method:
var ws:WebService = new WebService();
ws.wsdl("http://webserviceurl/wsdl");
ws.myMethod.addEventListener(event:ResultEvent, onMyMethodResultHandler);
ws.myMethod("input");
private function onMyMethodResultHandler(event:ResultEvent):void
{
// do something with the result of the web method call
}
In C#, you'd have to decorate your web methods with the WebMethod attribute:
[WebMethod]
public int myMethod(string input)
{
// do something with input
return 0; // return a result... just using int for simplicity's sake
}
Here's a link to a good tutorial on C# web services:
http://www.codeguru.com/csharp/csharp/cs_webservices/tutorials/article.php/c5477
Additionally, you can just send an http response to ActionScript, through the .aspx page's Response object. If you need to send complex data back, XML would probably be a more elegant solution than sending an array. Based off my previous example:
protected void Page_Load(object sender, EventArgs e)
{
int personID = int.Parse(Reqeust.QueryString["personID"]);
string personName = Request.QueryString["name"];
SendXmlResponse();
}
private void SendXmlResponse()
{
XmlDocument xDoc = new XmlDocument();
// do stuff here to build your XML document
// send the XML back
xDoc.Save(Response.OutputStream);
}
fx.barrett
11-06-2008, 09:26 PM
Thank, I'll look into ASP.NET web services then. And hopefully post an example once I get something working so that others can learn from a concrete example too. :) Thanks again.
EDIT: one question:
ws.myMethod.addEventListener(event:ResultEvent, onMyMethodResultHandler);
I know what you are trying to do there just that the syntax is a bit strange... from what I know, you can't add an event listener with event:ResultEvent and if I'm not mistaking then there is not such thing as ResultEvent in Flash ( no such default class I mean ).
Could you explain what's that and how to make it work because I got a webservice up but I really have no idea how to add that event listener...
fx.barrett
11-06-2008, 11:58 PM
Ok, nervermind, I finally got it working:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundGradientColors="[0xFFFFFF, 0xFFFFFF]">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
private function resultHandler(event:ResultEvent):void
{
this.message_tf.text += String(event.result);
}
]]>
</mx:Script>
<mx:WebService
id="myService"
wsdl="http://localhost/testApp/TestService.asmx?wsdl"
load="myService.HelloWorld()"
showBusyCursor="true"
result="resultHandler(event)"/>
<mx:Label id="message_tf" width="200" text="Result: "/>
</mx:Application>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace testApp
{
[WebService(Namespace = "http://wisebisoft.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class TestService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
And here's how you'd use the WebService object in an Script block:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundGradientColors="[0xFFFFFF, 0xFFFFFF]">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.soap.LoadEvent;
import mx.rpc.soap.mxml.WebService;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function clickHandler(event:MouseEvent):void
{
event.target.enabled = false;
var service:WebService = new WebService();
service.showBusyCursor = true;
service.addEventListener(ResultEvent.RESULT, resultHandler);
service.addEventListener(FaultEvent.FAULT, faultHandler);
service.loadWSDL("http://localhost/testApp/TestService.asmx?wsdl");
service.HelloWorld();
}
private function faultHandler(event:FaultEvent):void
{
Alert.show(String(event.fault), "Error");
this.loadBtn.enabled = true;
}
private function resultHandler(event:ResultEvent):void
{
this.message_tf.text += String(event.result);
}
]]>
</mx:Script>
<mx:VBox horizontalCenter="0" verticalCenter="0">
<mx:Label id="message_tf" width="200" text="Result: "/>
<mx:Button id="loadBtn" width="200" label="Load Service"
click="clickHandler(event)"/>
</mx:VBox>
</mx:Application>
yell0wdart
11-07-2008, 06:55 AM
Yea, that was my bad. LOL.
I pasted some of that code in from a tutorial I found and didn't have Flash or Flex in front of me to check the validity of the syntax... and I don't have the ActionScript development plugins for Visual Studio on my work lappy. Glad you got it figured out, though. ;)
/edit
Either solution would likely work for what you need. We actually use the XML solution at work to dynamically create a flash player with configurable settings. We just pass in the .aspx page path through flashvars and use the query string passed in to load the XML document.
I'd actually like to try the web service solution out myself (we haven't used them much at work). I have heard, though, that knowledge of SOA (service oriented architecture) is becoming more in demand.
fx.barrett
11-07-2008, 07:10 AM
Heh, as a next step, I'll try to see how ASP.NET deals with MS SQL because I saw that there are some really cool functions in ASP.NET for formatting data into XML. I essentially started reading up on ASP.NET yesterday and I didn't sleep tonight because I really got caught by it and I'm just experimenting and experimenting and experimenting ( I think that I'll start to rewrite some of my older projects done with PHP into ASP.NET just to practice it more :) )...
Everything seems fine until now and ASP.NET is really great. I never really liked PHP and I'm glad that I finally made this step, so, bye bye PHP.
yell0wdart
11-07-2008, 04:55 PM
A new convert. Mwahahaha! :p
ASP.NET has some really cool stuff right out of the box. The AJAX framework is nice, as it incorporates some of the basic functionality of C#.
If you're looking at getting into data access and SQL, it's good to learn ADO.NET as a lot of existing data access code is written that way. However, MS introduced LINQ (Language Integrated Query) with .NET 3.0, if I remember my versions correctly. It provides a standard method of data access, regardless of data source. You can LINQ to SQL, XML, Objects, etc, and basically treat all of that information polymorphically. Really slick stuff. I haven't had a chance to work with it yet, as our portal's data access layer is all written in ADO.NET. LINQ is definitely the future of data access in any .NET language, and I'm sure ADO.NET will probably get deprecated at some point in the not too distant future.
fx.barrett
11-07-2008, 07:17 PM
Agree, LINQ is really mind blowing and cool and I'm quite sure that in the future ( farer future ) all the companies will live up to this new language and maibe we'll be finally able to access data base informations from within our Flex applications ( not AIR ) without having to use any other server-side languages.
And yeah, I already jumped into ADO.NET last night and I'm still fooling around with it :) At the moment I decided to rewrite some of my tinier apps from PHP into ASP.NET and MS SQL for practice. Anyway, I'm really enjoying ASP.NET and MS SQL :D
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.