Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Referencing Script Libraries in views and using _Layout.cshtml redundant?

I you have a view that is using the _Layout.cshtml and you tell the view to Reference script libraries, is that redundant since you will be referencing the script libraries from the _Layout.cshtml or am I missing something here. In all of the MVC tutorials I have gone through from Microsoft, they always leave Reference Script Libraries checked.

like image 527
Xaisoft Avatar asked Jan 11 '12 14:01

Xaisoft


Video Answer


2 Answers

When you check "Reference Script Libraries" this just adds references to jquery.validate.min.js and jquery.validate.unobtrusive.min.js you would only need these javascript libraries on pages with forms as it validates what a user has entered.

You generally wouldn't reference these in the _Layout.cshtml as they are not needed on non-form pages and would be unnecessary HTTP request performed when loading the page.

If however you do have a form on every page or most pages and think it is acceptable to make the HTTP request on pages without forms then you can reference them in the _Layout.cshtml and simply not check the "Reference Script Libraries" when creating views.

like image 138
lancscoder Avatar answered Oct 05 '22 03:10

lancscoder


Yes, it is - referencing same script two times in both layout and view would result in script run two times in browser. Even if that was not error-prone, you actually would not want to waste browser resources in processing same script twice. Basically, those MVC tutotials are there to give you the basic knowledge and skills, not the production-ready code. In asp.net-mvc, you could use the great Cassette library, which does much more than helping script and stylesheet reference management - including minification, caching, compressing, combininig, etc..

like image 33
archil Avatar answered Oct 05 '22 03:10

archil