URL Rewrite on IIS 8 (.aspx to .svc)

My developers have been doing a lot of weird stuff with our WCF services and one of it, is to expose WCF services which uses WebHttpBinding to our external clients as .aspx. I know this is pretty weird, I find it pretty weird too but sometimes, we just have very adamant customers (or developers) who only want to see .aspx or they won't be able to sleep well at night *HeHe*.

Anyway, I'm happy that this can be achieved easily by using the URL Rewrite feature of IIS 8. For IIS 7 and 7.5, you will need to download it separately here. To enable URL Rewrites, simply include a section like this in your web.config file.
<system.webServer>
  <rewrite>
    <rules>
       <rule name="AspxToSvc">
          <match url="^MyService.aspx(.*)$" />
          <action type="Rewrite" url="MyService.svc/{R:1}" />
       </rule>
    </rules>
  </rewrite>
</system.webServer>  
The above rewrite rule basically rewrites any call to MyService.aspx to MyService.svc. If you prefer to use the UI, you can access it via the IIS Manager as well. Just look for a URL Rewrite icon in the Feature View.

Everything should work fine with the above configuration but if you are unlucky like me who may have messed up the IIS 8 installation earlier, here's what you can do. From IIS Manager, go to your web server and then open the Modules from Feature View.

Make sure you have a RewriteModule with the path %windir%\System32\inetsrv\rewrite.dll in there. If not, just click on Configure Native Modules... and add it in yourself.

With this, any service consumer that calls your REST-based WCF service will be able to access it with a .aspx extension. Of course my recommendation would be to do away with the extension if possible.

Note: If you are interested to make your WCF to be backward compatible with old-style applications that expects custom response formats from calling ASPX, you may want to read my post on WCF WebHttpBinding Custom WebMessageFormat.

No comments:

Post a Comment

Popular Post