Tải bản đầy đủ (.pdf) (28 trang)

aspnet information technology

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (569.46 KB, 28 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

Session 21



</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

Exploring ASP.NET / Session 21 / 2 of 28

Review



ASP.NET is a collection of all the ASP.NET pages, the custom



controls, and modules



It provides the configuration files to customize the web site.



The ASP.NET configuration files are written in XML, which enables



the developer to modify them using an XML parser.



Two types of configuration files exist in ASP.NET:



<b>Machine.Config </b>


<b>Web.Config</b>



A hierarchical structure can be formed with every directory,



</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

Exploring ASP.NET / Session 21 / 3 of 28

Review Contd…



Within the configuration tags, the configuration information is



grouped into two categories:



<b>The configuration section handler declaration area </b>


<b>The configuration section settings area</b>

.




The process of validating the user name and password is known as



</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

Exploring ASP.NET / Session 21 / 4 of 28

Objectives



Trace an ASP.NET application



Debug an ASP.NET application



Explain Data and Output caching



</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

Exploring ASP.NET / Session 21 / 5 of 28

Tracing



The process of locating



errors in a code is known as



<b>tracing</b>



Tracing is a new feature of



ASP.NET, as previous


versions of ASP did not


support tracing



Tracing in ASP.NET is



provided at two levels:




Page-Level Tracing



</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

Exploring ASP.NET / Session 21 / 6 of 28

Page-Level Tracing



The intrinsic controls like Trace are used to write



custom debugging statements



These statements appear at the end of the output that is



delivered to the client



Apart from just inserting statements at the end of the



clients’ output, ASP.NET also provides a few methods


like Init and Pre-Render, which print messages in the


trace information block of the page



To enable page level debugging for a page, the following



page directive has to be included at the beginning of the


page code.



</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

Exploring ASP.NET / Session 21 / 7 of 28

Page-Level Tracing Example



<% @Page Trace="True" TraceMode="SortByCategory" %>
<html>



<head>


<title>Page level Tracing</title>
</head>


<body>


<h1>A Program to test Trace...</h1>
</body>


</html>


</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

Exploring ASP.NET / Session 21 / 8 of 28

Trace Information Categories



<b>Category </b>

<b>Description </b>



<b>Request Details </b> <b>Lists all the information of the request </b>


<b>Trace Information </b> <b>Lists information from standard and </b>
<b>custom trace statements </b>


<b>Control Tree </b> <b>Lists all of the items in the page </b>


<b>Cookies Collection </b> <b>Lists all of the cookies of the page with </b>
<b>their values </b>


<b>Header Collection </b> <b>Lists all the items of the HTTP header </b>



<b>Form Collection </b> <b>Lists all of the form elements and their </b>
<b>values being POSTed. </b>


</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

Exploring ASP.NET / Session 21 / 9 of 28

Trace Example



<% @ Page Trace="True" TraceMode="SortByTime" %>
<html>


<head>


<title>Trace Methods</title>
</head>


<body>


<script language="C#" runat="server">
void Page_Init()


{


Trace.Warn ("Page Init Event: Message
from Page Init");


}


void Page_Load(Object Sender, EventArgs E)


{



</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

Exploring ASP.NET / Session 21 / 10 of 28

Trace Example - Output



</script>


<h1>A Program to test Trace...</h1>
</body>


</div>
<span class='text_page_counter'>(11)</span><div class='page_container' data-page=11>

Exploring ASP.NET / Session 21 / 11 of 28

Checking Trace



<b>… </b>



<b>if(Trace.IsEnabled) </b>


<b>{ </b>



<b> Trace.Warn (“Inside the Init proc! ”); </b>


<b>} </b>



<b>… </b>



Sometimes at runtime, it may be necessary to check if trace


in an ASP.NET page is enabled



</div>
<span class='text_page_counter'>(12)</span><div class='page_container' data-page=12>

Exploring ASP.NET / Session 21 / 12 of 28

Application Level Tracing



Used for tracing an entire application



Provides data about several requests to the pages of the




application at once



Allows the developer to view statistics and information on



the inputs of the page, and additional information with


respect to the entire application



Enables page-level tracing for all pages by default



Application level tracing can be enabled, by adding the



following line of code to the <system.web> section of


the web.config file



</div>
<span class='text_page_counter'>(13)</span><div class='page_container' data-page=13>

Exploring ASP.NET / Session 21 / 13 of 28


Application Level Tracing


Example



<configuration>
<system.web>


<b><trace enabled=“true” pageOutput=“true” /> </b>


</system.web>
</configuration>
Once this is coded, every
page will display trace
information.



Trace.axd file in the


application root directory
displays the information


</div>
<span class='text_page_counter'>(14)</span><div class='page_container' data-page=14>

Exploring ASP.NET / Session 21 / 14 of 28

Trace Tag Parameters



<b>Parameter</b> <b>Description</b> <b>Default Value</b>


requestLimit Sets the number of trace requests to store


on the server 10


traceMode Sets whether the statistics should be
arranged according to time or category
(takes in value sortByCategory or


sortByTime)


sortByTime


pageOutput Sets whether the trace information is to
be displayed at the bottom of every page
(takes in either true or false)


False


Enabled Sets whether application level tracing is to



be enabled. (takes in values true or false) True
localOnly Sets whether Tracing is enabled for


</div>
<span class='text_page_counter'>(15)</span><div class='page_container' data-page=15>

Exploring ASP.NET / Session 21 / 15 of 28

Debugging an Application



In spite of the tracing options provided by ASP.NET, bugs



may still creep into the programming logic of an


application



ASP.NET provides high-end debugging, that enables the



developer to step through the code line by line, and see


how the actual execution takes place



In order to configure the ASP.NET application to debug, a



</div>
<span class='text_page_counter'>(16)</span><div class='page_container' data-page=16>

Exploring ASP.NET / Session 21 / 16 of 28

Debugging an Application



<configuration>


<system.web>



<b><compilation debug="true"/> </b>



</system.web>


</configuration>




After configuring the application
for debug mode, the application
needs to be invoked by requesting
one of the application pages


After requesting a page, the


debugger has to be launched by
typing DbgClr.exe in the Run


</div>
<span class='text_page_counter'>(17)</span><div class='page_container' data-page=17>

Exploring ASP.NET / Session 21 / 17 of 28

Debug window with the code



 After invoking the debugger, the ASP.NET source file that needs to be debugged
should be opened


 The next step is to open the Processes dialog box, by clicking on the Tools


menu, and then clicking on Debug processes. The show system processes option
should be checked. The Aspnet_wp.exe process should also be attached.


 To set a breakpoint in an ASP.NET page, the left-hand margin on a line


containing an executable statement or method has to be clicked


 A red dot appears where the breakpoint is set.


 Moving the mouse over the breakpoint facilitates ensuring that the breakpoint,


is appropriately mapped to the correct application instance in the


Aspnet_wp.exe process


 The <b>Watch </b>window can be used to see the intrinsic objects on the page, like


</div>
<span class='text_page_counter'>(18)</span><div class='page_container' data-page=18>

Exploring ASP.NET / Session 21 / 18 of 28

Caching



Caching is a technique used to increase performance,



by keeping frequently accessed data in memory



In the case of a web application, caching is used to retain



pages or data across HTTP requests, and reuse them


without the expense of recompiling them



</div>
<span class='text_page_counter'>(19)</span><div class='page_container' data-page=19>

Exploring ASP.NET / Session 21 / 19 of 28

Output Caching



Output caching refers to caching the dynamic response



generated by a request



It is useful when the contents of an entire page are to



be cached



While a page is cached by the output cache, subsequent



requests for that page are served from the cached page



without executing the code that created it



The extent to which the cache will be stored in memory



can be set



The @OutputCache directive should be added at the



beginning of a page to cache a web page



The output cache directive takes in a duration



</div>
<span class='text_page_counter'>(20)</span><div class='page_container' data-page=20>

Exploring ASP.NET / Session 21 / 20 of 28

Output Caching Example



<b><%@ </b>OutputCache Duration="120<b>" </b>VaryByParam="none"<b> %></b>


<html>


<script language="C#" runat="server">
void Page_Load(Object Src, EventArgs E)
{


TimeGen.Text = DateTime.Now.ToString ("G");
}


</script>
<body>


<h1>Use of Output Cache</h1>



<p><i>This page was generated on:</i>
<asp:label id="TimeGen" runat="server"/>
</body>


</div>
<span class='text_page_counter'>(21)</span><div class='page_container' data-page=21>

Exploring ASP.NET / Session 21 / 21 of 28

Output Caching Example



More flexibility and control can be provided to the ASP.NET applications, by even
programmatically setting the duration of the cache


This can be achieved by importing the System.Web.HttpCachePolicy class, from
which the SetExpires(DateTime) method can be used.


Response.Cache.SetExpires(DateTime.Now.AddSeconds(120));
Another caching method is called <b>Sliding Expiration</b>, where the expiration
period is renewed by the value specified every time the page is accessed.


To make this sliding expiration policy, where the expiration time out resets each
time the page is requested, the SlidingExpiration property (of the class) can be
set as follows


</div>
<span class='text_page_counter'>(22)</span><div class='page_container' data-page=22>

Exploring ASP.NET / Session 21 / 22 of 28

Data Caching



Data caching provides a simple dictionary interface, that



allows programmers to easily place objects in, and


retrieve them from the cache




Placing an item in the cache is just like adding an item to



a dictionary



The syntax for doing this is as follows


Cache["mykey"] = myValue;



To retrieve the value, following syntax can be used



</div>
<span class='text_page_counter'>(23)</span><div class='page_container' data-page=23>

Exploring ASP.NET / Session 21 / 23 of 28

Data Caching Example



<%@ Import Namespace="System.Data" %>


<%@ Import Namespace="System.Data.SqlClient" %>
<html>


<head>


<title>Caching Data</title>
</head>


<script language="C#" runat="server">
void Page_Load(Object Src, EventArgs e)
{


DataView mySource;


<b>mySource = (DataView) Cache ["MyCache"]; </b>
<b>if (mySource == null) </b>



<b>{</b>


SqlConnection myConnection = new SqlConnection


("server=SQLDB; database=Northwind; uid=sa; pwd =password");


</div>
<span class='text_page_counter'>(24)</span><div class='page_container' data-page=24>

Exploring ASP.NET / Session 21 / 24 of 28

Data Caching Example



SqlDataAdapter myCommand = new SqlDataAdapter ("select * from
customers", myConnection);


DataSet myds = new Dataset();


myCommand.Fill(myds, "employee");


mySource = new DataView (myds.Tables["employee"]);<b> </b>
<b> Cache ["MyCache"] = mySource; </b>


<b> CacheMsg.Text = "Dataset created from Table";</b>
}


else
{


<b> </b> <b> CacheMsg.Text = "Dataset retrieved from Cache"; </b>


}



MyDataGrid.DataSource =mySource;
MyDataGrid.DataBind();


}


</div>
<span class='text_page_counter'>(25)</span><div class='page_container' data-page=25>

Exploring ASP.NET / Session 21 / 25 of 28

Data Caching Example



<body>


<form method="GET" runat="server">


<h3><font face="Verdana">Caching Data</font></h3>
<ASP:DataGrid id="MyDataGrid" runat="server" />


<p>


<i><asp:label id="CacheMsg" runat="server"/></i>
</form>


</div>
<span class='text_page_counter'>(26)</span><div class='page_container' data-page=26></div>
<span class='text_page_counter'>(27)</span><div class='page_container' data-page=27>

Exploring ASP.NET / Session 21 / 27 of 28

Deployment



Any application once developed needs to be deployed, or in



simple terms, should be made available to the user



Deploying a ASP.NET application is very simple.



The application directory and files need to be copied from




the development environment to the production


environment, where the application will go live.



XCOPY can be used to copy the files from source to



destination



The following syntax shows how to use the XCOPY



command to copy files



</div>
<span class='text_page_counter'>(28)</span><div class='page_container' data-page=28>

Exploring ASP.NET / Session 21 / 28 of 28

Summary



 The process of locating errors in the code is known as tracing.
 Tracing in ASP.NET is provided in two levels:


 Page-Level Tracing


 Application Level tracing


 Page Level Tracing displays a huge amount of information ranging from the timing when


each event was fired, to the timing when the cookies were created.


 Application level tracing by default turns on page level tracing for all the pages in the


application.



 The .NET Framework SDK includes a lightweight debugger that is perfectly suited for


debugging.


 Caching is a technique used to increase performance by keeping frequently accessed data


in the memory.ASP.NET supports two types of caching, they are:


</div>

<!--links-->

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×