views:

42

answers:

2

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.

+2  A: 

If 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.

bzlm
+1  A: 

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.

Gabe Moothart
Great. Is it normal to create subfolders in the Scripts folder for each subfolder in the Views folder so as to mimic the file directory?
Diego
why not - it could get messy if you anticipate many js files. maybe one for shared/common and one for each controller?
BritishDeveloper
As soon as you use an external component which uses subfolders, such as TinyMCE, you will *need* subfolders. So it's more common than not to have them. :)
bzlm