0

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" }
);

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

The next route handles the .js file so this answer is incorrect.
0

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)") }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.