开发者

ASP.NET MVC 2 NOT recognizing my javascript file

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


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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜