回答讨饭猫之asp.net优化(一)

asp.net|优化

Performance Tuning Tips

Any programming model has its common performance pitfalls, and ASP.NET is no exception. This section describes some of the ways in which you can avoid performance bottlenecks in your code.

  1. Disable Session State when not in use: Not all applications or pages require per-user session state. If it is not required, disable it completely. This is easily accomplished using a page-level directive, such as the following:
    <%@ Page EnableSessionState="false" %>

    Note: If a page requires access to session variables but does not create or modify them, set the value of the directive to ReadOnly. Session State can also be disabled for Web Service methods. See Using Objects and Intrinsics in the Web Services section.

  2. Choose your Session State provider carefully: ASP.NET provides three distinct ways to store session data for your application: in-process session state, out-of-process session state as a Windows Service, and out-of-process session state in a SQL database. Each has its advantages, but in-process session state is by far the fastest solution. If you are only storing small amounts of volatile data in session state you should use the in-process provider. The out-of-process solutions are primarily useful in Web garden and Web farm scenarios or in situations in which data cannot be lost in the event of a server/process restart.
  3. Avoid excessive round trips to the server: The Web Forms page framework is one of the best features of ASP.NET, because it can dramatically reduce the amount of code you need to write to accomplish a task. Programmatic access to page elements using server controls and the postback event handling model are arguably the most time-saving features. However, there are appropriate and inappropriate ways to use these features, and it is important to know when it is appropriate to use them.
    An application typically needs to make a round trip to the server only when retrieving data or storing data. Most data manipulations can take place on the client between round trips. For example, validating form entries can often take place on the client before the user submits data. In general, if you do not need to relay information back to the server, then you should not make a round trip to the server.
    If you are writing your own server controls, consider having them render client-side code for up-level (ECMAScript-capable) browsers. By employing "smart" controls, you can dramatically reduce the number of unecessary hits to your Web server.
  4. Use Page.IsPostback to avoid extra work on a round trip: If you are handling server control postbacks, you often need to execute different code the first time the page is requested from the code you do use for the round trip when an event is fired. If you check the Page.IsPostBack property, your code can execute conditionally, depending on whether there is an initial request for the page or a responce to a server control event. It might seem obvious to do this, but in practice it is possible to omit this check without changing the behavior of the page. For example:
        td.code {      padding:0,10,0,10;      border-style:solid;      border-width:1;      border-bottom:0;      border-top:0;      border-right:0;      border-color:cccccc;      background-color:ffffee    }    td.tab {      text-align:center;      font:8pt verdana;      width:15%;      padding:3,3,3,3;      border-style:solid;      border-width:1;      border-right:0;      border-color:black;      background-color:eeeeee;      cursor:hand    }    td.backtab {      text-align:center;      font: 8pt verdana;      width:15%;      padding:3,3,3,3;      border-style:solid;      border-width:1;      border-right:0;      border-color:black;      background-color:cccccc;      cursor:hand    }    td.space {      width:55%;      font: 8pt verdana;      padding:0,0,0,0;      border-style:solid;      border-bottom:0;      border-right:0;      border-width:1;      border-color:cccccc;      border-left-color:black;      background-color:white    }  
    <script language="C#" runat="server">    public DataSet ds;    ...    void Page_Load(Object sender, EventArgs e) {        // ...set up a connection and command here...        if (!Page.IsPostBack) {            String query = "select * from Authors where FirstName like '%JUSTIN%'";            myCommand.Fill(ds, "Authors");            myDataGrid.DataBind();        }    }    void Button_Click(Object sender, EventArgs e) {        String query = "select * from Authors where FirstName like '%BRAD%'";        myCommand.Fill(ds, "Authors");        myDataGrid.DataBind();    }</script><form runat="server">  <asp:datagrid datasource='<%# ds.DefaultView %>' runat="server"/><br>  <asp:button onclick="Button_Click" runat="server"/></form>
    <script language="VB" runat="server">    Public ds As DataSet    ...    Sub Page_Load(sender As Object, e As EventArgs)        ' ...set up a connection and command here...        If Not (Page.IsPostBack)            Dim query As String = "select * from Authors where FirstName like '%JUSTIN%'"            myCommand.Fill(ds, "Authors")            myDataGrid.DataBind()        End If    End Sub    Sub Button_Click(sender As Object, e As EventArgs)        Dim query As String = "select * from Authors where FirstName like '%BRAD%'"        myCommand.Fill(ds, "Authors")        myDataGrid.DataBind()    End Sub</script><form runat="server">  <asp:datagrid datasource='<%# ds.Tables["Authors"].DefaultView %>' runat="server"/><br>  <asp:button onclick="Button_Click" runat="server"/></form>
    <script language="JScript" runat="server">    public var ds:DataSet;    ...    function Page_Load(sender:Object, e:EventArgs) : void {        // ...set up a connection and command here...        if (!Page.IsPostBack) {            var query:String = "select * from Authors where FirstName like '%JUSTIN%'";            myCommand.Fill(ds, "Authors");            myDataGrid.DataBind();        }    }    function Button_Click(sender:Object, e:EventArgs) : void {        var query:String = "select * from Authors where FirstName like '%BRAD%'";        myCommand.Fill(ds, "Authors");        myDataGrid.DataBind();    }</script><form runat="server">  <asp:datagrid datasource='<%# ds.DefaultView %>' runat="server"/><br>  <asp:button onclick="Button_Click" runat="server"/></form>

    C# VB JScript
    The Page_Load event executes on every request, so we checked Page.IsPostBack so that the first query does not execute when we process the Button_Click event postback. Note that even without this check our page would behave identically, since the binding from the first query would be overturned by the call to DataBind in the event handler. Keep in mind that it can be easy to overlook this simple performance improvement when you write your pages.

  5. Use server controls sparingly and appropriately: Even though it is extremely easy to use, a server control might not always be the best choice. In many cases, a simple rendering or databinding substitution will accomplish the same thing. For example:
    <script language="C#" runat="server">    public String imagePath;    void Page_Load(Object sender, EventArgs e) {        //...retrieve data for imagePath here...        DataBind();    }</script><%-- the span and img server controls are unecessary...--%>The path to the image is: <span innerhtml='<%# imagePath %>' runat="server"/><br><img src='<%# imagePath %>' runat="server"/><br><br><%-- use databinding to substitute literals instead...--%>The path to the image is: <%# imagePath %><br><img src='<%# imagePath %>' /><br><br><%-- or a simple rendering expression...--%>The path to the image is: <%= imagePath %><br><img src='<%= imagePath %>' />
    <script language="VB" runat="server">    Public imagePath As String    Sub Page_Load(sender As Object, e As EventArgs)        '...retrieve data for imagePath here...        DataBind()    End Sub</script><%--the span and img server controls are unecessary...--%>The path to the image is: <span innerhtml='<%# imagePath %>' runat="server"/><br><img src='<%# imagePath %>' runat="server"/><br><br><%-- use databinding to substitute literals instead...--%>The path to the image is: <%# imagePath %><br><img src='<%# imagePath %>' /><br><br><%-- or a simple rendering expression...--%>The path to the image is: <%= imagePath %><br><img src='<%= imagePath %>' />
    <script language="JScript" runat="server">    public var imagePath:String;    function Page_Load(sender:Object, e:EventArgs) : void {        //...retrieve data for imagePath here...        DataBind();    }</script><%-- the span and img server controls are unecessary...--%>The path to the image is: <span innerhtml='<%# imagePath %>' runat="server"/><br><img src='<%# imagePath %>' runat="server"/><br><br><%-- use databinding to substitute literals instead...--%>The path to the image is: <%# imagePath %><br><img src='<%# imagePath %>' /><br><br><%-- or a simple rendering expression...--%>The path to the image is: <%= imagePath %><br><img src='<%= imagePath %>' />

    C# VB JScript
    In this example, a server control is not needed to substitute values into the resulting HTML sent back to the client. There are many other cases where this technique works just fine, even in server control templates. However, if you want to programmatically manipulate the control's properties, handle events from it, or take advantage of its state preservation, then a server control would be appropriate. You should examine your use of server controls and look for code you can optimize.

  6. Avoid excessive server control view state: Automatic state management is a feature that enables server controls to re-populate their values on a round trip without requiring you to write any code. This feature is not free however, since the state of a control is passed to and from the server in a hidden form field. You should be aware of when ViewState is helping you and when it is not. For example, if you are binding a control to data on every round trip (as in the datagrid example in tip #4), then you do not need the control to maintain it's view state, since you will wipe out any re-populated data in any case.
    ViewState is enabled for all server controls by default. To disable it, set the MaintainState property of the control to false, as in the following example:
    <asp:datagrid MaintainState="false" datasource="..." runat="server"/>

    You can also turn ViewState off at the page level. This is useful when you do not post back from a page at all, as in the following example:

    <%@ Page MaintainState="false" %>

    Note that this attribute is also supported by the User Control directive. To analyze the amount of view state used by the server controls on your page, enable tracing and look at the View State column of the Control Hierarchy table. For more information about the Trace feature and how to enable it, see the Application-level Trace Logging feature.

  7. Use System.Text.StringBuilder for string concatenation: Web applications often need to perform extensive string manipulation. Although standard string operators work for concatenation, they have a negative impact on performance. Using the System.Text.StringBuilder class to concatenate strings provides better performance, as demonstrated by the following example:
    // Using concatenation operators can sometimes produce// cleaner-looking code, but performance is not as good.String begin_query = "select UPPER(MachineName) As MachineName, "                   + "LOWER(MachineOwner) As MachineOwner, Status, "                   + "StartTime from NET_STRESS WHERE ";String end_query = " AND StartTime > '" + startTime + "' AND StartTime < '" + endTime + "'";String query = begin_query + GetWhereClause("PASSED") + end_query;// Consider replacing with StringBuilder instead:StringBuilder begin_query = new StringBuilder();begin_query.Append("select UPPER(MachineName) As MachineName ");begin_query.Append("LOWER(MachineOwner) As MachineOwner, Status, ");begin_query.Append("StartTime from NET_STRESS WHERE ");StringBuilder end_query = new StringBuilder();end_query.Append(" AND StartTime > '");end_query.Append(startTime);end_query.Append("' AND StartTime < '");end_query.Append(endTime);end_query.Append("'");String query = begin_query.Append(GetWhereClause("PASSED")).Append(end_query).ToString();
    ' Using concatenation operators can sometimes produce' cleaner-looking code, but performance is not as good.Dim begin_query As String = "select UPPER(MachineName) As MachineName, " _                          & "LOWER(MachineOwner) As MachineOwner, Status, " _                          & "StartTime from NET_STRESS WHERE "Dim end_query As String = " AND StartTime > '" & startTime & "' AND StartTime < '" & endTime & "'"Dim query As String = begin_query & GetWhereClause("PASSED") & end_query' Consider replacing with StringBuilder instead:Dim begin_query As New StringBuilderbegin_query.Append("select UPPER(MachineName) As MachineName ")begin_query.Append("LOWER(MachineOwner) As MachineOwner, Status, ")begin_query.Append("StartTime from NET_STRESS WHERE ")Dim end_query As New StringBuilderend_query.Append(" AND StartTime > '")end_query.Append(startTime)end_query.Append("' AND StartTime < '")end_query.Append(endTime)end_query.Append("'")Dim query As String = begin_query.Append(GetWhereClause("PASSED")).Append(end_query).ToString()
    // Using concatenation operators can sometimes produce// cleaner-looking code, but performance is not as good.var begin_query:String = "select UPPER(MachineName) As MachineName, "                   + "LOWER(MachineOwner) As MachineOwner, Status, "                   + "StartTime from NET_STRESS WHERE ";var end_query:String = " AND StartTime > '" + startTime + "' AND StartTime < '" + endTime + "'";var query:String = begin_query + GetWhereClause("PASSED") + end_query;// Consider replacing with StringBuilder instead:var begin_query:StringBuilder = new StringBuilder();begin_query.Append("select UPPER(MachineName) As MachineName ");begin_query.Append("LOWER(MachineOwner) As MachineOwner, Status, ");begin_query.Append("StartTime from NET_STRESS WHERE ");var end_query:StringBuilder = new StringBuilder();end_query.Append(" AND StartTime > '");end_query.Append(startTime);end_query.Append("' AND StartTime < '");end_query.Append(endTime);end_query.Append("'");var query:String = begin_query.Append(GetWhereClause("PASSED")).Append(end_query).ToString();

    C# VB JScript

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, server
, append
, query
, stringbuilder
, query end
, to
, query load
The
,以便于您获取更多的相关知识。

时间: 2025-01-20 14:03:40

回答讨饭猫之asp.net优化(一)的相关文章

asp.net优化(二)

asp.net|优化 Do not rely on exceptions in your code: Exceptions are very expensive and should rarely occur in your code. You should never use exceptions as a way to control normal program flow. If it is possible to detect in code a condition that would

Asp编码优化技巧_应用技巧

8条Asp编码优化技巧: 1.声明VBScript变量      在ASP中,对vbscript提供了强劲的支持,能够无缝集成vbscript的函数.方法,这样给扩展ASP的现有功能提供了很大便利.由于ASP中已经模糊了变量类型的概念,所以,在进行ASP与vbscript交互的过程中,很多程序员也惯于不声明vbscript的变量,这样加重了服务器的解析负担,进而影响服务器的响应请求速度. 鉴于此,我们可以象在VB中强制用户进行变量声明一样在vbscript中强制用户进行变量声明.实现方法是在AS

读《ASP.NET优化》有感

导读:本文转自博客园,作者根据<ASP.NET优化>一书,介绍关于敏捷开发和编程的实质---测试 ,部署以及代码管理.方便开发者的需要. 以下是文章内容: 这本书主要是讲ASP.NET优化开发的,里面关于面向对象编程的介绍和ASP.NET的开发介绍可以说在一般的书之上,由于我从来没有涉足过这方面的内容,所以不予很深的评论.但是第十六章,讲了一下测试 ,部署以及代码管理,这方面和我们现在的工程和接近,读着很有感觉. 现在就来讲讲测试.里面说到"敏捷开发和极限编程的实质之一是基于测试的开

ASP 程序优化

程序|优化     优化是每个开发人员应该关心的问题.对于数据库访问,优化是一个关键问题.和其他任务相比,数据的访问显得相对慢些.       因为数据访问的变化是如此之多,以致于几乎不可能提出一套固定的数据库操作的优化规则.通常碰到这类问题,经常得到这样的回答:"这取决于--",因为这类优化问题取决于准备做什么. 9.3.1 常用的ADO技巧       尽管优化取决于所执行的任务,但是仍然有一些常用的技巧:       · 仅选择所需的列.当打开记录集时,不要自动地使用表名(即SE

ASP.NET优化:Sql注入和Html注入的黑帽SEO

黑帽(black hat)SEO主要是指采取"不怎么道德"(暂时就这么形容吧!)的方式进行搜索引擎优化. 1. 注入攻击,包括Sql注入和Html注入.我经常能看到对Sql注入防范的谈论,但对于Html注入,很多人并没有引起足够的重视.为了展示Html注入的效果,我们模仿了一个常见的留言本功能. 首先,在页面声明中添加两个属性设置EnableEventValidation="false" ValidateRequest="false" ,这很关键

ASP中优化数据库处理

ASP是一个Web服务器端的开发环境,它提供了一种简单易学的脚本(VBScript或Jscript),并带有许多内置的对象,从而提供了一条简捷的编程之路.更为重要的是,ASP中提供了ADO对象,让程序员可以轻松操作各种数据库,从而可以产生和运行动态的.交互的Web服务应用程序. 目前,国内很多电子商务站点都采用了ASP技术来与数据库交互,为用户提供各类服务. 由于电子商务站点的大部分信息都存放在数据库中,要提高Web的响应速度.建立高性能的电子商务站点,很大一部分取决于ASP与数据库之间的处理性

微软建议的ASP性能优化28条守则

微软|性能|优化|微软|性能|优化   目录技巧 1:将经常使用的数据缓存在 Web 服务器上技巧 2:将经常使用的数据缓存在 Application 或 Session 对象中技巧 3:将数据和 HTML 缓存在 Web 服务器的磁盘上技巧 4:避免将非敏捷的组件缓存在 Application 或 Session 对象中技巧 5:不要将数据库连接缓存在 Application 或 Session 对象中技巧 6:合理地使用 Session 对象技巧 7:将代码封装在 COM 对象中技巧 8:迟

在ASP中优化数据库处理的实现

数据|数据库|优化 ASP是一个Web服务器端的开发环境,它提供了一种简单易学的脚本(VBScript或Jscript),并带有许多内置的对象,从而提供了一条简捷的编程之路.更为重要的是,ASP中提供了ADO对象,让程序员可以轻松操作各种数据库,从而可以产生和运行动态的.交互的Web服务应用程序. 目前,国内很多电子商务站点都采用了ASP技术来与数据库交互,为用户提供各类服务. 由于电子商务站点的大部分信息都存放在数据库中,要提高Web的响应速度.建立高性能的电子商务站点,很大一部分取决于ASP

ASP编码优化

编码|优化 ASP(Active Server Page)是Microsoft公司推出的基于PWS(Personal Web Server)&IIS(InternetInformation Server)平台的.基于ISAPI(InternetServiceAPI)原理的动态网页开发技术,目前日趋成熟完善.在这里仅就代码优化进行一些简单讨论. 1. 声明VBScript变量 在ASP中,对vbscript提供了强劲的支持,能够无缝集成vbscript的函数.方法,这样给扩展ASP的现有功能提供了