Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Adding *.js Routes to *.aspx Files

Adding *.js Routes to *.aspx Files

Scheduled Pinned Locked Moved ASP.NET
javascriptsysadmintoolshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    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

    Richard DeemingR 1 Reply Last reply
    0
    • S Skippums

      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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 your web.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 the UrlRoutingModule-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 or IHttpHandlerFactory class to handle the requests. You might be able to get away with using System.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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups