简体   繁体   English

ASP.NET MVC3中特定于Razor视图的JQuery代码的最佳实践

[英]Best practices for Razor view-specific JQuery code in ASP.NET MVC3

I've read a bit about including js libraries and using RenderSection to dynamically and optionally load extra libraries on demand. 我已经阅读了一些关于包含js库和使用RenderSection来动态地和可选地按需加载额外库的内容。

To date, I've seen most view-specific scripts included directly inside script tags inside the view files themselves. 到目前为止,我已经看到大多数特定于视图的脚本直接包含在视图文件本身内的script标记内。 Is there a different/better way to do this? 有没有不同/更好的方法来做到这一点?

I think it's best to place all your JS files in a section att the end of your "_Layout.cshtml". 我认为最好将所有JS文件放在“_Layout.cshtml”末尾的一个部分中。

http://developer.yahoo.com/performance/rules.html http://developer.yahoo.com/performance/rules.html

But notice that all your JS files will be fint at all your pages then. 但请注意,您的所有JS文件都将在您的所有页面上都是fint。 If you want som JS files in only one our a few of your pages, you can add a new section for this JS files. 如果您只想在我们的几个页面中使用som JS文件,则可以为此JS文件添加新的部分。 And set this section required attribute to false. 并将此部分必需属性设置为false。

It's usually best to make sure that you're only including scripts that are relevant for that particular page. 通常最好确保您只包含与该特定页面相关的脚本。

So including jQuery in the _Layout page is a good idea if you're using it on every page but you may only need jQuery validate on a page that actually has inputs to validate. 因此,如果您在每个页面上使用jQuery,那么在_Layout页面中包含jQuery是一个好主意,但您可能只需要在实际具有要验证的输入的页面上进行jQuery验证。 Otherwise you're just downloading an extra file you don't need. 否则你只是下载一个你不需要的额外文件。 Always remember that any unnecessary HTTP requests can be costly. 永远记住,任何不必要的HTTP请求都是昂贵的。

The use of RenderSection here is appropriate, even if it is just to organize it. 这里使用RenderSection是合适的,即使只是为了组织它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM