ASP.NET 提速20条

当你开发程序的时候有些东西是你应该要考虑的。在12年的asp和asp.net工作中我积累了一

些避免或者使用某些方法来使程序的性能表现明显提升。下面是20条改善asp.net程序性能的

建议:
 1.禁用Session State
  如果你不打算使用Session State 就禁用它,它默认是可用的。你可以在

具体的页面禁用而不是所有页面:


   你也可以通过在web.config 设置<sessionState>为off的方法来关闭

Session State
 2.输出缓冲
  这个方法的最大好处就是可以批量的在服务器上处理你的工作,然后通过

Response.Flush 方法(向客户端发送当前所有缓冲的输出,将当前所有缓冲的输出强制发送

到客户端。在请求处理的过程中可多次调用 Flush)输出数据,这样避免了来回的与服务端

交互
  <%response.buffer=true%>

  Then use:
 
  <%response.flush=true%>
 3.避免服务端验证

 4.Repeater Control Good,  DataList, DataGrid, and DataView controls Bad
  Repeater相当不错
 5.在进行大量操作之前利用HttpResponse.IsClientConnected 带来的好处
  HttpResponse.IsClientConnected(获取一个值,通过该值指示客户端是

否仍连接在服务器上)

 

  if  (Response.IsClientConnected)
               
{
                   
// If still connected, redirect
                   
// to another page. 
                   Response.Redirect("Page2CS.aspx"false);
               }

               
else
               
{
                   
// If the browser is not connected
                   
// stop all response processing.
                   Response.End();
               }


  6.使用HTTPServerUtility.Transfer 而不是使用 Response.Redirect
  Redirect方法总是与服务端交互,它们应该被使用在服务器间的跳转,对于

任何服务器内的跳转应该使用.transfer!这样你会节省很多不必要的http请求。
 7.使用Validator控件时经常检查Page.IsValid
  确保处理表单之前检查Page.IsValid
 8.在release模式下部署
  在你部署你的网站的时候确保使用的是release编译模式而不是debug编译

模式,Debug模式下生成的程序集为调试版本,未经优化;在bin/debug/目录中有两个文件,

除了要生成的.exe或.dll文件外,还有个.pdb文件,这个.pdb文件中就记录了代码中的断点

等调试信息;Release模式下不包含调试信息,并对代码进行了优化,/bin/release/目录下

只有一个.exe或.dll文件。

 9.关闭Tracing
  Tracing非常棒,虽然如此有人建议过你关闭它吗?如果没有你一定要通过

修改web.config来关闭它,它会给你的程序带来一些额外的开销
               

  < configuration >
                    
< system .web >
                        
< trace  enabled ="false"  pageOutput ="false"   />
                        
< trace  enabled ="false"  requestLimit ="10"  

pageOutput
="false"  traceMode ="SortByTime"  localOnly ="true" />
                        
< compilation  debug ="false"   />
                   
</ system.web >
                
</ configuration >

 

 10.Page.IsPostBack是你的朋友
  确保不执行不需要执行的代码,我不知道有多少开发人员忘记IsPostBack

,但它对我来说是最基本的
 11.避免异常
 12.Caching 或许是第一号tip!
  Use Quick Page Caching and the ASP.net Cache API!  Lots to learn,

its not as simple as you might think.  There is a lot of strategy involved here. 

When do you cache?  what do you cache?
 13.Create Per-Request Cache
  Use HTTPContect.Items to add single page load to create a per-

request cache.
 14.StringBuilder  
  StringBuilder.Append比String + String要更快。虽然要使用

stringbuilder你必须
  new StringBuilder(),
  因此当字符串不是很多,连接不超过3次你仍然可以坚持使用String +

String,或者可以试试String.Concat
 15.关闭ViewState
If you are not using form postback, turn off viewsate, by default, controls will

turn on viewsate and slow your site.
  如果你不准备postback表单,关闭viewsate, 控件viewsate默认是可用的

,这个会使你的网站变慢

  public  ShowOrdersTablePage()
{
    
this .Init  +=   new  EventHandler(Page_Init);
}
 
private   void  Page_Init( object  sender, System.EventArgs e)
{
    
this .EnableViewState  =   false ;
}

 

 16.使用分页
  利用.net分页的简便,每次只显示一部分的数据可以加载的更快。只是需

要注意一下缓存,,不要把所有数据都缓存到grid中,试着想一下:如果把在google中搜索

music的所有数据缓存,这些数据将会有多大 ;)
 17.使用AppOffline.htm
  我讨厌asp.net的报错页面,如果我永远看不到这些页面我将会非常高兴。

如果你也不想看到这些页面,使用AppOffline.htm
 18.Use ControlState and not ViewState for Controls
  Detail:http://msdn2.microsoft.com/en-us/library/1whwt1k7.aspx
 

 19.使用finally方法
  如果你与数据库有连接确保在最后关闭它们!Finally 方法正式你所需要

的,因为它是代码执行的唯一的地方

 20.Option Strict and Option Explicit
This is an oldy, and not so much a strictly ASP.net tip, but a .net tip in general.  Make sure you turn BOTH on.  you should never trust .net or any compiler to perform conversions for you.  That's just shady programming, and low quality code anyway.  If you have never turned both on, go turn them on right now and try and compile.  Fix all your errors.

 

There are hundreds more where these came from, however I really feel that these are the most critical of the speed improvements you can make in ASP.net that will have a dramatic impact on the user experience of your application.  As always if you have any suggestions or tips to add, please let us know!  We would love to hear them! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值