Adding *.js Routes to *.aspx Files
-
I need to have some server-generated javascript files, so I decided to create *.aspx pages that return the application/javascript mime type. This part is working, but now I am attempting to add a route to the routing table so the client is unaware of the technology used to generate the javascript file (e.g., I want to reference a *.js file from the client, then route the request to the appropriate *.aspx page). I added the following to RouteConfig.cs:
routes.MapPageRoute("Enumerations", "Scripts/Constants/Enumerations.js",
"~/Scripts/Constants/Enumerations.aspx");When I include the script using the *.aspx file, everything works as expected. However, when I attempt to access the *.js file, I get a 404/Not Found error. Is there a way to make the file publicly available as a *.js file?
Sounds like somebody's got a case of the Mondays -Jeff
-
I need to have some server-generated javascript files, so I decided to create *.aspx pages that return the application/javascript mime type. This part is working, but now I am attempting to add a route to the routing table so the client is unaware of the technology used to generate the javascript file (e.g., I want to reference a *.js file from the client, then route the request to the appropriate *.aspx page). I added the following to RouteConfig.cs:
routes.MapPageRoute("Enumerations", "Scripts/Constants/Enumerations.js",
"~/Scripts/Constants/Enumerations.aspx");When I include the script using the *.aspx file, everything works as expected. However, when I attempt to access the *.js file, I get a 404/Not Found error. Is there a way to make the file publicly available as a *.js file?
Sounds like somebody's got a case of the Mondays -Jeff
The issue is that IIS sees a request for a
*.js
file, which isn't mapped to an ASP.NET handler, and so it doesn't invoke the ASP.NET pipeline. The routing module never gets the chance to see the request and route it to your real handler. There seem to be three basic options to fix this: 1.runAllManagedModulesForAllRequests="true"
Modify yourweb.config
to tell IIS to invoke the full ASP.NET pipeline on every request.<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>This will potentially have an impact on the performance of your site. 2. Remove the
preCondition
on theUrlRoutingModule-4.0
module.<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
</system.webServer>The performance impact won't be as great as option 1, but there will still be an impact, as the routing module will now fire for every request to the site. 3. Custom handlers Explicitly map the requests to an ASP.NET handler:
<system.webServer>
<handlers>
<add
name="dynamic-js" verb="GET,HEAD" path="scripts/*/*.js"
type="DynamicScripts.CustomScriptHandler, DynamicScripts"
preCondition="integratedMode"
/>
</handlers>
</system.webServer>This will have better performance that the other two options. The down-side is that you have to create an
IHttpHandler
orIHttpHandlerFactory
class to handle the requests. You might be able to get away with usingSystem.Web.UI.PageHandlerFactory
, but I haven't tested it. :) http://svenaelterman.wordpress.com/2011/01/31/using-asp-net-4-0-extension-less-routing-on-iis-7-5/[^] http://www.hanselman.com/b