`

20 Tips to Improve ASP.net Application Performance

阅读更多
August 31, 2007
20 Tips to Improve ASP.net Application Performance
Not a .net Developer?
Are you an asp.net developer?  If you aren't don't worry, we have similar posts in the works for Ruby, PHP, and other developers out there.  If you are an ASP.net developer, listen up!

Get Ready for Massive Gains
There are certain things you should take into account when you are developing your applications.  Over the last 12 years or so of working with asp and asp.net, I have learned to avoid and do certain things that increase your application performance by a massive amount!  Below are my top 20 tips to improving ASP.net application Performance.

Disable Session State
Disable Session State if you're not going to use it.  By default it's on. You can actually turn this off for specific pages, instead of for every page:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1"
EnableSessionState="false" %>
You can also disable it across the application in the web.config by setting the <sessionState> mode value to Off.

Output Buffering
Take advantage of this great feature.  Basically batch all of your work on the server, and then run a Response.Flush method to output the data.  This avoids chatty back and forth with the server.

<%response.buffer=true%> Then use:

<%response.flush=true%> Avoid Server-Side Validation
Try to avoid server-side validation, use client-side instead. Server-Side will just consume valuable resources on your servers, and cause more chat back and forth.

Repeater Control Good,  DataList, DataGrid, and DataView controls Bad
Asp.net is a great platform, unfortunately a lot of the controls that were developed are heavy in html, and create not the greatest scaleable html from a performance standpoint.  ASP.net  repeater control is awesome!  Use it!  You might write more code, but you will thank me in the long run!

Take advantage of HttpResponse.IsClientConnected before performing a large operation:

if (Response.IsClientConnected)
        {
            // If still connected, redirect
            // to another page.
            Response.Redirect("Page2CS.aspx", false);
        }What is wrong with Response.Redirect? Read on...

Use HTTPServerUtility.Transfer instead of Response.Redirect
Redirect's are also very chatty.  They should only be used when you are transferring people to another physical web server.  For any transfers within your server, use .transfer!  You will save a lot of needless HTTP requests.

Always check Page.IsValid when using Validator Controls
So you've dropped on some validator controls, and you think your good to go because ASP.net does everything for you!  Right? Wrong!  All that happens if bad data is received is the IsValid flag is set to false. So make sure you check Page.IsValid before processing your forms!

Deploy with Release Build
Make sure you use Release Build mode and not Debug Build when you deploy your site to production. If you think this doesn't matter, think again.  By running in debug mode, you are creating PDB's and cranking up the timeout.  Deploy Release mode and you will see the speed improvements.

Turn off Tracing
Tracing is awesome, however have you remembered to turn it off? If not, make sure you edit your web.config and turn it off!  It will add a lot of overhead to your application that is not needed in a production environment.

<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>Page.IsPostBack is your friend
Make sure you don't execute code needlessly. I don't know how many web developers forget about checking IsPostBack!  It seems like such a basic thing to me!  Needless processing!

Avoid Exceptions
Avoid throwing exceptions, and handling useless exceptions. Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications.  Write your code so they don't happen!  Don't code by exception!

Caching is Possibly the number one 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?

Create Per-Request Cache
Use HTTPContect.Items to add single page load to create a per-request cache.

StringBuilder
StringBuilder.Append is faster than String + String.  However in order to use StringBuilder, you must
new StringBuilder()Therefore it is not something you want to use if you don't have large strings.  If you are concatenating less than 3 times, then stick with String + String. You can also try String.Concat

Turn Off ViewState
If you are not using form postback, turn off viewsate, by default, controls will turn on viewsate and slow your site.

public ShowOrdersTablePage()
{
    this.Init += new EventHandler(Page_Init);
}

private void Page_Init(object sender, System.EventArgs e)
{
    this.EnableViewState = false;
}Use Paging
Take advantage of paging's simplicity in .net. Only show small subsets of data at a time, allowing the page to load faster.  Just be careful when you mix in caching.  How many times do you hit the page 2, or page 3 button?  Hardly ever right!  So don't cache all the data in the grid! Think of it this way: How big would the first search result page be for "music" on Google if they cached all the pages from 1 to goggle ;)

Use the AppOffline.htm when updating binaries
I hate the generic asp.net error messages!  If I never had to see them again I would be so happy.  Make sure your users never see them!  Use the AppOffline.htm file!

Use ControlState and not ViewState for Controls
If you followed the last tip, you are probably freaking out at the though of your controls not working.  Simply use Control State.  Microsoft has an excellent example of using ControlState here, as I will not be able to get into all the detail in this short article.

Use the Finally Method
If you have opened any connections to the database, or files, etc, make sure that you close them at the end!  The Finally block is really the best place to do so, as it is the only block of code that will surely execute.

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!

Have web development!

分享到:
评论

相关推荐

    Beginning ASP.NET 2.0 in C# 2005:From Novice to Professional

    This book provides thorough coverage of ASP.NET, guiding you from beginning to advanced techniques, such as querying databases from within a web page and performance-tuning your site. You'll find tips...

    Bootstrap for ASP.NET MVC

    Bootstrap, a leading open source... At the end of this book, you will find some valuable tips and tricks, which will help you in getting the most out of your Bootstrap and ASP.NET MVC integrated website.

    Beginning ASP.NET 4 in VB 2010

    Within these pages, you’ll find tips for best practices and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also ...

    jquery操作asp.net中GridView方法

    用户可通过:http://www.jquerybyexample.net/2013/07/jquery-gridview-aspnet-tips-and-tricks.html 下载 Formatting Related Formatting ASP.NET GridView using jQuery Highlight row on mouseover in ...

    Professional Refactoring in C# & ASP.NET

    In this first book to provide a hands-on approach to refactoring in C# and ASP.NET, you’ll discover to apply refactoring techniques to manage and modify your code. Plus, you’ll learn how to build a...

    ASP.NET 2.0 MVP Hacks and Tips.chm

    ASP.NET 2.0 MVP Hacks and Tips byDavid Yacket al. Wrox Press 2006 (428 pages) ISBN:0764597663 Written with the seasoned professional in mind, this survival guide offers little-known solutions, ...

    一个基于ASP.NET 的js客户端处理技术JavaScript Tips 之一

    本文将深入探讨基于ASP.NET的JavaScript客户端处理技术,并分享一些实用的JavaScript Tips,帮助开发者提升应用程序的性能和用户体验。 首先,了解JavaScript在ASP.NET中的作用至关重要。在服务器端,ASP.NET处理...

    Beginning ASP.NET 4 in C# 2010

    Within these pages, you’ll find tips for “best practices” and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also ...

    Bootstrap for ASP.NET MVC, 2nd Edition

    At the end of this book, you will find some valuable tips and tricks to help you get the most out of your Bootstrap- and ASP.NET MVC-integrated website. What You Will Learn Create a new ASP.Net MVC...

    Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional (1)

    Within these pages, you’ll find tips for “best practices” and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also ...

    Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional (3)

    Within these pages, you’ll find tips for “best practices” and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also ...

    Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional (2)

    Within these pages, you’ll find tips for “best practices” and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also ...

Global site tag (gtag.js) - Google Analytics