开发者

Getting ASP.NET root path to Flex?

I am working on a project that is primarily ASP.NET based. The main project is meant to be deployed to multiple locations for different clients, so one client might be located at website.com/client1 and another at website.com/client2. Within the application, we regularly use the application root operator ~ to get the path to a resource.

We also have a bunch of Flex applications that get deployed in there, and many rely on web services within the ASP.NET application. What I'm after is a way to reference the services relative to the application root. Here's an example of the location of some files for two client deployments:

  • Client A
    • website.com/clientA/swf/FlexApplication.swf
    • website.com/clientA/services/webService.asmx
  • Client B
    • website.com/clientB/swf/FlexApplication.swf
    • website.com/clientB/services/webServi开发者_高级运维ce.asmx

FlexApplication and webService are both exactly the same, so what I want to do is something like this in the Flex code:

var myService:CustomService = new CustomService(~/services/webService.asmx);
myService.callMethod("Test");

I would like to avoid using relative paths for the usual reasons. Is there a good way to do this or a good way to pass the root url to the flex application from ASP.NET? Thanks in advance.


Its definitely a good idea to avoid relative URL's. The easiest way is to pass the information in via Flash Vars in the HTML embed statement.

Check out Adobe's documentation on using FlashVars with Flex: http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html

Summary:

Add a line to the HTML embed statement like this:

<param name='flashVars' value='serviceRoot=/myserviceRoot'/>

Then access it in Flex via the mx.core.Application.application.parameters accessor

import mx.core.Application;

var parameters:Object = Application.application.parameters;
var serviceRoot:String = parameters['serviceRoot'];


relative to the application root

Flex knows nothing about the application root of a ASP.NET application. The only thing that the Flex app knows is the URL that it is served from. It does not know the URL of the page. Keep in mind that the URL of the page and the URL of the SWF are not the same.

It appears, given your directory structure, that you can use the SWF's URL to get the information you're after.

In a Flex 3 Application, you can get use the url property of the Application tag:

(Application.application as Application).url 

In a Flex 4 Spark application, you can also use the url property of the Application Tag, but you have to get it differently:

(FlexGlobals.topLevelApplication as Application).url 

Then you can parse it with URLUtils to get your directory structure. Probably store that as a global variable in your Flex application somehow and use it to construct the URL for the service calls you are making to the remote server.


I use this handy little utility to get URL information via javascript inside the .swf.

http://www.flexpasta.com/index.php/category/utility-classes-that-help-you-deal-with-string-numbers-arrays-and-objects-in-actionscript/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜