开发者

Correct external javascript-per-view file placement in an ASP .NET MVC Application

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开发者_如何学运维.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜