i am working on an asp.net MVC web application , currently i am managing the scripts as follow:-
1. i have placed the following scripts on the _layout view since they are used by most of the view (but of course not all the views):-
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.14.min.js")" type="text/javascript"></script>
- then i have added the other scripts to the related views themself if they need them.
so my question is if this approach will achive better performance since i am not going to load all the scripts on the _layout view?
Yes, this is best practice. But just be mindful, if you are using a certain
scriptfile in a majority of the Views, then I would place that in your _Layout.cshtml as well. Just because it isn’t used in 100% of the Views, doesn’t mean it isn’t practical and advised to put it in your layout View.If there are a few or two that uses are particular script, you are right: Just put it in that View. But remember by doing this you are repeating yourself, and it could potentionally be a maintenance nightmare if you need to update the script file name or path.
It’s a gray area, but I’d be willing to say you are doing the right thing.