3

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 Answers 2

3

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.

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

3 Comments

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?
why not - it could get messy if you anticipate many js files. maybe one for shared/common and one for each controller?
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. :)
3

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.

Comments

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.