Cross domain AJAX call using IIS 7 and Microsoft URL Rewrite

I recommend

I created a WCF web service that I wanted to host on a web site and domain different from my main TheThoughtfulCoder.com domain. The problem is AJAX calls can not be made to different domain names, even if they are sub domains.

I'm using dasBlog to host my main blog site and I don't want to put my web services in the same project. I want to keep my web services completely separate. So making a call from thethoughtfulcoder.com to a web service hosted on the domain services.thethoughtfulcoder.com can not be done. Hmmm...bummer!

I found a way to make this work in a straight forward manner. At least I think it is :-)

IIS 7 has an addon called Application Request Routing which allows IIS to proxy calls to another web site to satisfy cross domain rewrite rules. Ah ha! Now I can rewrite a reqeust to a service on my main site and on the web server rewrite it to my services subdomain. To set this up I followed the steps listed below.


Download and install Application Request Routing from Microsoft.
Go to the web server machine home in IIS Manager. The machine home is the root node of the web site in the Connections tree panel. Go into the Application Request Routing feature. ARR Snippet

In the Application Request routing configuration, check the Enable Proxy checkbox. I'm using the default application request routing settings. ARR Enable Proxy Snippet

I created a rewrite rule on the TheThoughtfulCoder.com that treats the directory http://TheThoughtfulCoder.com/ttcservce as the place holder for the web service. The rewrite rule will rewrite any URL with /ttcservice/ to the web service http://services.thethoughtfulcoder.com/TTCService.svc. Since application request routing is enabled on the web server the rewrite rule will cause IIS to make a proxy call to the other domain! In case you're curious I'm using host headers to separate the web sites hosted on a single web server.Service proxy rewrite rule The above rewrite configuration will generate the following XML in the web.config file:
<rule name="TTC-Service Rewrite" stopprocessing="true"> 
   <match url="^ttcservice/(.*)$"> 
   <conditions logicalgrouping="MatchAll">  
   <action type="Rewrite" url="http://services.thethoughtfulcoder.com/TTCService.svc/{R:1}">
   </action> 
   </conditions> 
   </match> 
</rule>

If you have any questions or need any clarification please let me know and I'll try to clarify the steps. Of course there are likely many other ways to accomplish making cross domain AJAX calls, but this solution might be good enough :-)

IIS Technical September 8, 2009


Comments