Getting ASP.NET root path to Flex? - asp.net

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/webService.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/

Related

Cannot route static files in ASP.NET WebForms

We have legacy code to maintain and, to solve a specific customer customization problem, we want to route calls to some files to other files. That is, when the app calls a particular ASPX, it will end up hitting another ASPX.
If you call:
www.foo.com/admin/admin.aspx
It will actually hit:
www.foo.com/customizations/customer1/admin/admin.aspx
This is not a good design but this is legacy code. We just want to solve this.
We are using the System.Web.Routing framework to solve it. This works fine when you set RouteExistingFiles to true, except for static files (CSS, JavaScript and Images).
When I first tried it, it retrieved this error:
There is no build provider register for the extension '.css'.
So I did register a build provider in the web.config file for the .css extension. I used this build provider: PageBuilderProvider because someone recommended it in the internet.
It works! But the CSS is being served with text\html content type.
How do I achieve this?
TL;DR: I want to use routes in ASP.NET Web Forms to make a call for a specific CSS file to actually retrieve another one. A customer needs this for customization.
Try coding a HttpHandler. I had to do something similar but for PDF files, I coded a custom HttpHandler in the end - works very well. You can even set the content type in the HttpHandler code and have a pattern matched path the handler will be used for in the web.config. You can also configure it in web.config not to execute if the path does not point to an existing file e.g. so a 404 is returned without having to code that in the handler itself. I can't post my code (VB.NET) ATM because I'm using a tablet but google search for tutorials. You will also probably need to use the TransmitFile function to actually write out the css file. Is it a web forms project or web site? If its a web site there is a special way of registering the HttpHandler in the web.config.

How do I get the ASP.NET content relative path

Doing a project between multiple people, and a few components (web app, services app and some others). We will be storing some information inside the Content folder of the web app so it can be accessed directly from the web server with an href, however other components outside of the web app need to access this folder as well, and since we are sharing the project between multiple people using an absolute path is not an option. What options do we have?
EDIT: Trying to explain it a little better.
What i have exactly is, a web project, a "data project" which is just a dll, a "logic" project which is another dll and a services project which is an exe/service.
Both the web project and service project consumes the methods from the logic, and the logic from the data project. Being the last one the responsable for storing data (in a database) and also in the file system.
This "filesystem" path should be configurable, and we are aiming to put it into the content folder of the web project so multimedia files can be accessed directly rather than doing a byte stream.
Now in the web.config(config file of the web app), and app.config(config file of the services app) i could set the absolute path to web/content (the same for both config files) and the data dll would use it without problems. Now the main problem is that we cannot put an absolute path in the config file because each person works on a different computer with obviously different file paths. So if i could just write something like: ~/project/Web/Content rather than C:/myfolder/stuff/blabla/project/web/content in the config files, with ~ resolving the path to the project, this is what i want! Or maybe better ideas about how to share a folder with these apps without adding absolute paths hardcoded somewhere.
What you want to use is:
Server.MapPath("/Content/filepath.ext");
This will give you the absolute path of a file based on it's position within the website, in this case, from the /Content directory.
For a program external to the website, you have a couple options;
The easiest to implement might be a simple configuration value in the external program which points to the directory. My guess is you've already decided that's not ideal, but it may be the quickest way.
Alternatively, there's a Microsoft .NET assembly which gives you easy access to IIS information (I can't recall its name off the top of my head!). You could use this assembly to find the appropriate website, and retrieve its root directory. I'll see if I can find it and get an example, or maybe someone else will see this and post an answer with that information.
Please check the following method "ResolveClientUrl"
MSDN
Use the ResolveClientUrl method to return a URL string suitable for use by the client to access resources on the Web server, such as image files, links to additional pages, and so on.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx

HTTP auth and Flex

I have a small query. I wrote a Flex Application with PHP remoting using ZendAMF. i also made and auth system. User provides credentials and i pass them to my gateway where i have a service registered to query a SQL db and verify if the user is registered or not. Thats working perfectly. In my application i am loading MRTG graphs (PNG Files) into Image component. The GRAPHS are inside my Document Root. They are also loading well. Problem is if i type the URL path to the png file i can see it directly without any authentication. to cater that i added htaccess file to MRTG folder inside my document root. Know when i view the images inside my Flex App it asks for HTTP username and password. which i dont want.
In simple words . MRTG PNG's are inside my document root
Can i move them out of document root and still have my Flex App access them ( i tried and failed with that)
I just want the user to be able to view MRTG pngs and not directly from URL.
If you are using ZendAMF, why don't you just create a service method on your service that provides you with those images?
That way you have full control over what happens.
For example you could send them through as a byte array or a base64 encoded string, making it a lot harder to define where the image is actually stored on server disk.
Cheers

How can I configure my data services in Flex to call the same domain that the SWF was served from?

I've got an HTTP service I defined in Flash Builder, via the "Data Services" tab. I've got an absolute URL in there right now.
What I really want is to not define a path that includes a domain name at all--I want the service to simply call an absolute path that's on the same domain as whatever domain the SWF was served from... can I do that? When I got rid of the base URL and then gave an absolute URL path (e.g., /roster/deleteMember), Flex Builder complained that "File does not exist." Well, of course it doesn't exist, it isn't a file, it's a URL to a service call--there is no corresponding file on the filesystem.
Can anyone advise me how to do that? If I change the domain name, it wipes out all the parameter definitions for the methods, so I have to go back to each method and setup the parameters again. Rather a headache.
Now, I would have thought this would work. from the adobe documentation:
The configuration files sometimes contain special {server.name} and {server.port} tokens. These tokens are replaced with server name and port values based on the URL from which the SWF file is served when it is accessed through a web browser from a web server. Similarly, a special {context.root} token is replaced with the actual context root of a web application.
So, if you specify the endpoint as
http://{server.name}:{server.port}/{context.root}, then
automatically on runtime, the variables are set by the flashplayer depending on where you've been downloaded the application.
Sounds great... but it's not working for me. I can't even set those values in the Flash Builder Data Services tool. Here's a recording of what I'm getting.
http://screencast.com/t/MTk0NzNiYzY
I'm not sure it's possible from the DS window.
If you were doing it in code, you could use Application.application.url to get where the swf had been loaded from.

Flex modulleader url, why cant it just look at the root?

We are using pureMVC multicore as the flex Framework,
When we need to load a module we use the first line "moduleEval.swf";.
It works well in local for testing.
But not on the production server which is obvioulsy online, in which case i need to put he full URL.
Is that normal ?
moduleLoader.url = "moduleEval.swf";
//moduleLoader.url = "http://www.foobar.com/site_media/mymodule.swf";
Thanks,
Greg
I almost always use absolute urls, without the site name. The URL base directory is wherever your root application is. So if you load a module within another app, and that module loads another or something, any relative urls are relative to the base app.
moduleLoader.url = "/site_media/moduleEval.swf";

Resources