开发者

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.

To date, I've seen most view-specific scripts included directly inside script tags inside the view files themselves. Is there a different/bette开发者_开发技巧r 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".

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

But notice that all your JS files will be fint at all your pages then. 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. And set this section required attribute to 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. Otherwise you're just downloading an extra file you don't need. Always remember that any unnecessary HTTP requests can be costly.

The use of RenderSection here is appropriate, even if it is just to organize it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜