I'm working on an ASP .NET Core MVC application. On previous projects, I would put any page-specific JavaScript at the bottom of my View's .cshtml
file. However, this project has more page-specific JavaScript and some have recommended using separate .js
files, one per View. I've had trouble finding recommended patterns. One downside of separate .js
files is that it's harder to inject data from the ViewModel
or ViewBag
which are available in .cshtml
.
What are best practices for managing JavaScript in ASP .NET MVC apps?
We are considering the following, where each view would reference its respective .js
file. But this still has the downside of not being able to access the ViewModel
, ViewBag
, and other helpers.
[wwwroot]
- [js]
- [src]
- [mycontroller] // controller folders
- index.js // view js files
- about.js
- [othercontroller]