Question

Near the bottom of my routing registration, if a URL has a .js extension, I'd like to handle it with a particular controller (most .js content is served statically, but some is special and needs to be served via a controller). However, the following route is being skipped, and the catch-all route is handling the request.

routes.MapRoute("ContentScript", "{script}.js",
   new { controller = "Content", action = "Script" },
   new[] { "NameSpace.Controllers" }
);

What is the right way to do this?

In the route directly after that one every remaining request is routed like so (and this works and catches the .js files), so the issue is not in any part but the url parameter):

routes.MapRoute("ContentScript", "{*path}",
   new { controller = "Content", action = "Index" },
   new[] { "NameSpace.Controllers" }
);

I also tried the following, without success:

routes.MapRoute("ContentScript", "{*script}",
   new { controller = "Content", action = "Script" },
   new { script = new RegexConstraint("\\.js$") },
   new[] { "NameSpace.Controllers" }
);
Was it helpful?

Solution 2

The problem was an IgnoreRoute that I was unaware of (the RouteRegistry.cs file is 1,469 lines long... I have not studied it in its entirety, yet). js files are being handled in managed code--they were just being taken out by this before my route could handle the request.

routes.IgnoreRoute("{*path}",
   new { path = new RegexConstraint(@"[^?]*\.(gif|jpe?g|png|ico|js|swf|css|txt|html?|xml|pdf)") }
);

OTHER TIPS

you have to add handler to the web.config so it can handle it ... some thing like that :

<system.webserver>
    <handlers>
    <add name="scripts" path="*.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    </handlers>
</system.webserver>

stackoverflow link #1

stackoverflow link #2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top