0

RESOLVED: I had to comment _routes.RouteExistingFiles = true; and it started recognizing my .js files.

I don't understand why my ASP.NET MVC 2 application is NOT recognizing my java script files. I tried following ways to include my scrip but it does not recognize.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="<%= Url.Content("~/JS/JScript1.js") %>"></script> 
<script src="../../JS/JScript1.js" type="text/javascript"></script>

JScript1.js is located at <root>/JS/JScript1.js

I get following error in my ControllerFactory:

The controller for path '/JS/JScript1.js' was not found or does not implement IController.

Here are my route settings:

    _routes.RouteExistingFiles = true;
    _routes.IgnoreRoute("{file}.txt");
    _routes.IgnoreRoute("{file}.htm");
    _routes.IgnoreRoute("{file}.html");
    _routes.IgnoreRoute("{file}.xml");
    // Ignore axd files such as assest, image, sitemap etc
    _routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    // Ignore the assets directory which contains images & css
    _routes.IgnoreRoute("Content/{*pathInfo}");
    //Exclude favicon (google toolbar request gif file as fav icon)
    _routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });
    _routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });
2
  • Could you post your routes from global.asax? Commented Apr 19, 2011 at 13:52
  • David, I added my route settings to my question. Commented Apr 19, 2011 at 13:56

2 Answers 2

1

It looks like one of your routes is matching the path to your JS file. You will need to do something like this:

routes.IgnoreRoute("JS/*.js");

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

1 Comment

I tried placing the js file in some other folder and got the same error.
0

Your route is mapping as follows:

/JS/JScript1.js
/{controller}/{action}/[empty string]

because that is the first route which matches it. Since you do not have a controller named JSController, you are getting the error. You have a few options:

  • ignore routes containing the extension .js
  • use the content folder which the framework lets things flow through by default
  • remove the default route and add routes for each of your controllers manually
  • put your js file at least 3 folders deep so it no longer matches the default route (i.e. /level1/level2/level3/JScript1.js)

3 Comments

Nick, I commented "_routes.RouteExistingFiles = true;" and it started recognizing my .js files. Do you see anything wrong with this approach?
@Alex: For what you are doing, it's probably not an issue, though I would advise against it because what that does is tell ASP.NET to handle the response on existing files and not the MVC engine. This is probably more useful for combining webforms and MVC in the same project. You might want to try directly routing to your view files and see what happens.
Thank you. I'll try your suggestion and will keep you posted.

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.