【asp源码栏目提醒】:网学会员,鉴于大家对asp源码十分关注,论文会员在此为大家搜集整理了“ASP NET代码优化 - 技术总结”一文,供大家参考学习!
ASP NET代码优化 阅读1评论0作者luoht发表于2010-04-11 1716原文链接 This article gives asimple checklist that can simply be run against any ASP.NET Web Page to optimize it Introduction This article gives asimple checklist that can simply be run against any ASP.NET Web Page to optimize it.This is in no way acomplete listhowever it should work for most of the web pages.For aperformance critical application you will surely need to go beyond this checklist and will require amore detailed plan. Checklist for all pages Here is the list of checks that you need to runnot necessarily in order Disable ViewState-SetEnableViewStatefalsefor any control that does not need the view state.As ageneral rule if your page does not use postbackthen it is usually safe to disable viewstate for the complete page itself.Use Page.Ispostback is used in Page_Load-Make sure that all code in page_load is withinifPage.Ispostbackunless it specifically needs to be executed upon every Page Load.Asynchronous calls for Web Services-If you are using Web Services in you page and they take long time to loadthen preferably use Asynchronous calls to Web Services where ever applicable and make sure to wait for end of the calls before the page is fully loaded.But remember Asynchronous calls have their own overheadsso do not overdo it unless needed.Use String Builder for large string operations-For any long string operationsuse String Builder instead.Specialized Exception Handling-DO not throw exceptions unless neededsince throwing an exception will give you aperformance hit.Instead try managing them by using code likeif not system.dbnull.Even you if you have to handle an exception then de-allocate any memory extensive object withinfinallyblock.Do not depend on Garbage Collector to do the job for you.--ifvml----endif--Collapse // //Try //Create Db Connection and call aquery //sqlConnNew SqlClient.SqlConnectionSTR_CONN //Catch ex As Exception //Throw any Error that occurred //Throw ex //Finally //Free Database connection objects //sqlConnNothing //End Try // Leave Page Buffering on-Leave Page buffering Onunless specifically required so.Places where you might require to turn it on in case of very large pages so that user can view something while the complete page is loading.Use Caching-Cache Data whenever possible especially data which you are sure wont change at all or will be reused multiple times in the application.Make sure to have consistent cache keys to avoid any bugs.For simple pages which do not change regularly you can also go for page Caching--ifvml----endif--Collapse // //OutputCache Duration60VaryByParamnone // Learn more aboutVaryByParamandVaryByControlfor best results. Use Script files-As arule unless required do not insert JavaScript directly into any pageinstead save them as script file.jsand embed them.The benefit being that common code can be shared and also once ascript file is loaded into browser cacheit is directly picked up from browser cache instead of downloading again.Remove Unused Javascript-Run through all Javascript and make sure that all unused script is removed.Remove Hidden HTML when using Tabstrip-In you are using aTabstrip control and if the HTML size is too large and the page does frequent reloadsthen turn Autopostback of Tabstrip onput each Pageview inside apanel and turn visibility of all Panels except the current one to False.This will force the page to reload every time atab is changed however the reload time will reduce heavily.Use your own jurisdiction to best use.Additional Ch ecklist for performance critical pages Disable session when not using it-If your page does not use session then disable session specifically for that page.--ifvml----endif--Collapse // //Page EnableSessionStatefalse // If the page only reads session but does not write anything in sessionthen make it read only. --ifvml----endif--Collapse // //Page EnableSessionStateReadOnly // Use Option Strict OnVB.Net only-Enabling Option Script restricts implicit type conversionswhich helps avoid those annoying type conversion and also is aPerformance helper by eliminating hidden type conversions.I agree it takes away some of you freedombut believe me the advantages outweigh the freedom.Use Threading-When downloading huge amounts of data use Threading to load Data in background.Be awarehoweverthat threading does carry overhead and must be used carefully.A thread with ashort lifetime is inherently inefficientand context switching takes asignificant amount of execution time.You should use the minimum number of long-term threadsand switch between them as rarely as you can.Use Chunky Functions-A chunky call is afunction call that performs several tasks.you should try to design your application so that it doesnt rely on smallfrequent calls that carry so much overhead.Use Jagged Arrays-In case you are doing heavy use of Multi Dimensional Arraysuse Jagged ArrayArrays of ArraysInsteadUseinstead of-You should use the concatenation operatorinstead of the plus operatorto concatenate strings.They are equivalent only if both operands are of type String.When this is not the casetheoperator becomes late bound and must perform type checking and conversions.Use Ajax-In performance critical application where there are frequent page loadsresort to Ajax.Use the SqlDataReader class-The SqlDataReader class provides ameans to read forward-only data stream retrieved from aSQL Serverdatabase.If you only need to read data then SqlDataReader class offers higher performance than the DataSet class because SqlDataReader uses the Tabular Data Stream protocol to read data directly from adatabase connectionChoose appropriate Session State provider-In-process session state is the fastest solution.If you store only small data in session statego for in-process provider.The out-of-pro cess solutions is useful if you scale your application across multiple processors or multiple computers.Use Stored Procedures-Stored procedures are pre-compiled and hence are much faster than adirect SQL statement call.Use Web Services with care-Web Services depending on data volume can have monstrous memory requirements.Do not go for Web Services unless your Business Models demands it.Paging in Database Side-When you needs to display large amount of Data to the usergo for astored procedure based Data Paging technique instead of relying on the Data Grid/Data List Paging functionality.Basically download only data for the current page.原文地址评论0查看评论发表评论 找优秀程序员就在博客园 最新新闻·优视科技手机浏览器的三条获利路2010-04-11 1621·传漾科技贩卖剩余流量2010-04-11 1615·大佬的赌局暗战移动支付2010-04-11 1612·索尼剥离制造工厂一个典型日企的自我救赎2010-04-11 1606·微软将发布11个漏洞补丁程序用户注意下载2010-04-11 1605 编辑推荐重新认识C玩转指针 网站导航博客园首页个人主页新闻闪存小组博问社区知识库