[ASP. NET Notes] 9. Tracking. caching. configuring applications

Source: Internet
Author: User
Tracking
1) with asp.net, users can view the request details on an asp.net page.
Contains information about all Web controls in the form, including session, cookie, and application.
2) with the asp.net tracking technology, you can quickly understand the execution process and details of an asp.net page.
Help us eliminate errors on the page
3) Tracking Classification
A: page-level tracking: <% @ page trace = "true" tracemode = "specified mode" %>
B: Trace. warn ("info") can be used to display custom messages in the Trace results, which are displayed in red.
C: application-level tracking: All pages of a Web application can be tracked.
Configure in the web. config file
<Configration>
<System. web>
<Trace enabled = "true" pageoutput = "true"/>
</System. web>
</Configration>

Cache
What is cache:
1) the cache technology will store frequently accessed or large amounts of data in the memory.
2) You do not need to create a new one during future access. You can directly go from the cache.
3) the caching technology can significantly improve the program performance.
4) Remember: Once the application is stopped or restarted and re-compiled, the cache will disappear.
Cache category:
A: Output cache:
Cache the entire page.
1) <% @ OutputCache Duration = "120"> indicates saving the entire page for 120 seconds.
Write the following code in the Load event of the page:
Response. write (DateTime. now. tostring ());
Result: The page is continuously accessed within 120 seconds, and the time is not changed because
Cache. However, if you access the service in 120 seconds, the time will change,
Because the cache will be updated, go to the page from the server. But it remains unchanged in the next 120 seconds.
B: data cache:
Cache is not the whole page, but every data in the page. Usually the query results from the database
Large data volumes and frequently accessed data are stored in the data Cache, which is implemented by the Cache class.
The format is as follows:
1) Cache: if (Cache ["key"] = null) Cache ["key"] = Value
2) data retrieval: variable name = (type conversion) Cache ["key"];
However, the fatal weakness of this format is that the cache will not change as the data in the cache is updated, and the real-time data is not high.
You can use the following code to automatically notify the cache that the cache has expired when the data in the cache changes, thus recreating the cache.
Write the following code in the Page_load event:
If (! IsPostBack)
{
Response. write ("start data acquisition time:" + DateTime. now. toLongTimeString () + "millisecond:" + DateTime. now. millisecond. toString () + "<br> ");
DataTable dt = null;
If (Cache ["dt"] = null)
{
String constr = ConfigurationManager. ConnectionStrings ["constr"]. ConnectionString; // read web. config configuration information
Dt = dbManager. RunHasResultSql ("select * from worker ");
SqlCacheDependencyAdmin. EnableNotifications (constr); // enable the automatic notification feature for Web Applications
SqlCacheDependencyAdmin. EnableTableForNotifications (constr, "worker"); // sets the automatic notification feature for tables in the database.
SqlCacheDependency sqldep = new SqlCacheDependency ("northwind", "worker"); // create a worker table in the northwind for cache clearing, in the future, as long as the data in the worker table changes, the cache will be cleared.
Cache. Insert ("dt", dt, sqldep); // Insert Cache items
Response. Write ("fetch data from database <br> ");
}
Else
{
Dt = (DataTable) Cache ["dt"];
Response. Write ("Remove from cache <br> ");
}
This. GridView1.DataSource = dt;
This. GridView1.DataBind ();
Response. write ("end data acquisition time:" + DateTime. now. toLongTimeString () + "millisecond:" + DateTime. now. millisecond. toString () + "<br> ");
}

Configuration File: Web. config file
**************************************** **************************************** *************
<Configuration>
<AppSettings/>
<ConnectionStrings>
<Add name = "constr" connectionString = "server =.; database = northwind; uid = sa; pwd =" providerName = "using System. Data. SqlClient"/>
</ConnectionStrings>
<System. web>
<Caching>
<SqlCacheDependency enabled = "true" pollTime = "500"> // polltime is the polling time. When the data in the cache changes in the background, it will notify the cache expiration unit of milliseconds for too long, it must be at least> = 500, so this item must not be less
<Databases>
<Add name = "northwind" connectionStringName = "constr"/>
</Databases>
</SqlCacheDependency>
</Caching>

**************************************** **************************************** *************


The application configuration of Asp.net is stored in the Web. Config configuration file.
Web. Config configuration can increase Program Flexibility

1: How to use Web. Config to configure database connection strings
1) configure the following in the <connectionStrings> section:
<ConnectionStrings>
<Add name = "constr" connectionString = "server =.; database = test; uid = sa; pwd = 123" providerName = "System. Data. SqlClient">
</Add>
</ConnectionStrings>
2) read the configured string:
String constr = ConfigurationManager. ConnectionStrings ["constr"]. ConnectionString;
2: When an application error occurs, it is automatically redirected to an error page.
<CustomErrors mode = "On" defaultRedirect = "Error. aspx"/>
3: Enable mode:
<Compilation debug = "true"/>
It can be enabled during development, but must be disabled during release.
4: Specify the Asp.net Authentication mode. Four test modes
A) Windows Authentication Mode:
Each time the client connects to the Web server, you must enter windows
Account name, so that IIs will think you have the permission to access the Web page on the Web server
(Cannot be tested on the local machine. Because the local machine has been logged on when it is started, that is, it has passed
Window identity verification)
Benefits: It can be well integrated with the company's server account to ensure security.
Disadvantage: any user who wants to browse the Web page must create an account
Conclusion: If your website is accessible from the Internet, you cannot use this Authentication mode.
Will create many accounts
B) Forms authentication (Internet authentication)
Each time the client connects to any page of the Web server, the Web server checks whether the Web server passes the Forms authentication. If the Web server fails, the Web server automatically goes to the user-developed logon form for identity verification. Once the user
After verification, a cookie is added to the client, and then the cookie is used for identity
Inspection. This allows you to log on only once and then always trust your account. Previous logon methods
As long as you know the Web Page name, you can directly enter it in the web site, which is very insecure.
Steps:
<Authentication mode = "Forms">
<Forms name = "myauth" loginUrl = "login. aspx"> </forms>
</Authentication>

<Authorization>
<Deny users = "? "> </Deny> [deny unauthorized accounts]
</Authorization>
Enter the following code on the logon page:
If (this. TextBox1.Text = "chen" & this. TextBox2.Text = "123 ")
{
FormsAuthentication. SetAuthCookie (this. TextBox1.Text, false); [write Cookies. use false and true to determine whether to always trust or only trust this logon]
If (FormsAuthentication. GetRedirectUrl (this. TextBox1.Text, false). EndsWith ("default. aspx") // if the user directly opens the logon page, the default. aspx is automatically returned.
{
Response. Redirect ("index. aspx"); // you will be redirected to the homepage.
}
Else
{
FormsAuthentication. RedirectFromLoginPage (this. TextBox1.Text, false); // jump to the page with the last error
}
}
Example:
1) add multiple links to the same page, including product management, customer management, product management, and login.
Product management, customer management, and product management modules are available only after a user has logged on,
Otherwise, the link will be automatically displayed on the logon page.
2) Prevent users from knowing the address of a webpage and directly enter the webpage to run the website
C) Other Passport and None must be configured with other servers, which can only be used with third-party software and are rarely used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.