ASP.NET MVC - Where do you put your .js files if you dont want to store them in /Scripts?

纵饮孤独 提交于 2019-12-05 09:51:09

SOLVED

After having found and tested a number of posts (most conclusive post here) that I couldnt get to work, the following solves the puzzle:

In the Global.asax, add the following code to the RegisterRoutes method:

routes.IgnoreRoute("{file}.js");

There is a great post here that describes this and additional activity here

You have two options, I think.

  1. Recreate the Views folder structure under scripts and store them in the same relative location in the scripts directory.
  2. Don't keep them in separate files, but use separate ContentPlaceHolders in the MasterPage for a script section (typically at the bottom of the body).

You could also store them in any of the non-"special" folders, but I think scripts is the right place if you are keeping them separate.

I normally keep common scripts (used by more than one page) in the scripts folder, without a corresponding views hierarchy, and page-specific scripts in the view file, but in a separate ContentPlaceHolder. To me the important thing is not to keep the JS in a separate file, but separate from the content. Using a different ContentPlaceHolder accomplishes this.

Contemplate that last sentence of yours - might that be why a specific directory (/Scripts/) has been set aside for these? ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!