In an ASP .NET MVC application, what's the correct folder to place external javascript files particular to each view? Most views require javascript code that I'm planning to write in external files, but I'm not sure if I should drop them next to the views or in subfolders of the Scripts folder.
views:
42answers:
2If you put them in the Views folder, then that folder needs to support direct access from the web. Normally, that is not the case, and in a default ASP.NET MVC project, the Views folder even has a Web.config
that prohibits access. Views are supposed to be resources used by Controllers when rendering data to a client.
The Scripts
folder is best. You can reference them with <%= Url.Content("~/Scripts/dir/script.js") %>
The basic MVC folder structure is to group things by type - Controllers all together, Views all together, Scripts all together, etc. Use a consistent naming convention so you can easily locate the correct script.
A benefit of this approach is that you can easily move your scripts to a separate static-file server, if the need arises, because they are all under the same root. If you write your own replacement for Url.Content
, you can even do this with only a code change to one function.