Getting Classic ASP to work in .js files under IIS 7

aspasp-classiciis-7javascript

I am moving a clients classic asp webapp to a new IIS7 based server. The site contains some .js files which have javascript but also classic asp in <% %> tags which contains a bunch of conditional statements designed to spit out pieces of javascript based on session state variables. Here's a brief example of what the file could be like….

var arrHOFFSET = -1;
var arrLeft ="<";
var arrRight = ">";


<% If ((Session("dashInv") = "True") And ((Session("systemLevelStaff") = "4") Or (Session("systemLevelCompany") = "4"))) Then  %>
    addMainItem("/MgmtTools/WelcomeInventory.asp?wherefrom=salesMan","",81,"center","","",0,0,"","","","","");
<% Else %>
    <% If (Session("dashInv") = "False") And ((Session("systemLevelStaff") = "4") Or (Session("systemLevelCompany") = "4")) Then %>
    <% Else %>
        addMainItem("/calendar/welcome.asp","",81,"center","","",0,0,"","","","","");
    <% End If %>
<% End If %>

defineSubmenuProperties(135,"center","center",-3,0,"","","","","","","");

Currently this file (named custom.js for example) will start throwing js errors, because the server doesnt seem to recognize the asp code in it and therefore does not parse it. I know I need to somehow specify that a .js file should also be treated like an .asp file and run through parsing it. However I am not sure how to go about doing this.

Here is what I've tried so far…
Under the Server node in IIS under HANDLER MAPPINGS I created a new Script Map with the following settings.

Request Path: *.js
Executable: C:\Windows\System32\inetsrv\asp.dll
Name: ASPClassicInJSFiles
Mapping: Invoke Handler only if request is mapped to : File
Verbs: All verbs
Access: Script

I also created a similar handler under the site node itself.

Under MIME Types .js is defined as application/x-javascript

None of these work. If I simply rename the file to have .asp extension then things work, however this app is poorly coded and has literally 100's of files with the .js files included in them under various names and locations, so rename, search and replace is the last option I have.

Best Answer

I think your steps are correct. I just tried this on the site level in IIS7 and 7.5 in both cases it works and the classic ASP code is executed correctly.

Not sure what's wrong with your setup. Double-check that the script mapping is not below the StaticFile mapping by view the Ordered List.

Also if you are on a 64 Bit OS, make sure the application pool is 64Bit, if "Enable 32 Bit Application" is set to true, you need to use the 32 Bit Version of the asp.dll (C:\Windows\SysWOW64\inetsrv\asp.dll)