Asp. NET notes ViewState and cache use _ Practical skills

Source: Internet
Author: User
Tags system log

1,

(1, by default, ASP.net is enabled viewstate, so that in the page will generate lengthy hidden fields, viewstate for the need to postback processing of the page can be useful, for the news display page does not need to interact with the viewstate is absolutely unnecessary.

(2, the way to disable ViewState:
• Page Overall disable viewstate: Enableviewstate= "False" in the top page
• Specifies that the control disables ViewState and enableviewstate= "False" on the control

(3, the page is disabled viewstate after not completely remove ViewState, as long as ViewState is not very big on it. If you require a little viewstate can not have, then the page can not have runat=server form, if the page does not have a FORM element, the form completely removed can be. If the server-side control such as button is not placed in the Runat=server form, then it is not available.

2, Cache

(1, If every time you enter the page to query the database to generate page content, if the traffic is very large, the site performance will be very poor.) And if only the first visit to query the database to generate page content, and then directly output content, you can improve system performance. This way, no matter how many people are accessing the database only once, the database pressure does not change.

Caching is a space-time technology that exists in many parts of the computer and is used to store commonly used data in slow-speed devices in fast devices, taking data directly from a fast device. such as CPU level two cache, Windows file read cache.

The problem with caching failure: to ensure that data is read from the cache and consistent with data in slow data, the corresponding data in the cache needs to be purged when the corresponding data in the slow data changes.
caching is the first way to improve the performance of a Web site , just as an index is the first way to improve database performance .
asp.net cache is mainly divided into: page caching, data source caching, data caching of these three main types.

(2, Page caching

Add the following label to the page to enable page caching.

Copy Code code as follows:

<%@ OutputCache duration= "No" varybyparam= "None"%>

The contents of the entire page will be cached, the ASP.net code in the page, the data source will not be run during the cache, but directly output the cached page content. Duration represents the cache time, in seconds, over which the cache is invalidated, and the next generation is cached for 20 seconds, and so on. Set breakpoints at Page_Load and modify database data tests.
The cache is for all visitors to this page. The pressure on the database is the same for 1 visitors and 10,000 visitors, one access, and 1 million visits.

* * * for reading news page, if the setting, it will be cached in the first to see the news, because? id=2,? id=3 is just different parameters of the page, in order to allow different news to each cache, so you can set the varybyparam= "id" that represents a separate cache for different ID parameters . If there are multiple parameters that determine the cache, separate the parameter names with semicolons, such as varybyparam= "Id;number".
If you want to create a different cache for any of the different query strings , set varybyparam= "*", which is usually sufficient to set "*".
You can also set the cache of a control like a page cache in Webusercontrol.

(3, data source cache
Set the ObjectDataSource CacheDuration (cache time: seconds),enablecaching=true. This will call the SelectMethod specified method to execute the database query every cacheduration specified time period , and all other times the cached data will be returned directly.

The cache fixed time is applicable to pages with frequent visits, such as the homepage, the list of articles, etc. for the view of the page is not suitable, assuming that there are 1 million posts, if each post is fixed cache 1 hours, assuming that within an hour of 100,000 posts were seen, then we will cache 100,000 posts, very occupy memory, because "hundred Years a look "The grave posts" were occasionally accessed once also cached for one hours, consuming memory. At this time, the "sliding window (sliding)" policy, such as the post cache 10 minutes, if the 10 minutes to access, then the expiration of the cache will be changed from the moment of access to the 10 minutes, and so on. This frequently visited posts can be "long-term cache", and infrequently visited posts will not be due to accidental access to occupy the cache for a long time. Set method, data source: cacheexpirationpolicy= "Sliding". Interview can be talked about. TODO: There seems to be a problem with sliding. Not a problem, sliding is just a policy, the server will refer.

(4, Cache other
Page caching, data source caching, etc. are used to implement the cache, in some page caching, the data source cache can not complete the special cache requirements, the direct call to the Httpruntime.cache cache Httpruntime.cache.

(*) The asp.net cache is saved in memory by default and can also be configured to be saved to the database. Large Web sites will also be used in conjunction with technology such as memcached.
Clears the cache. The cache may need to be emptied immediately when the cache is not invalidated, so that changes to the database are immediately reflected in the interface. Asp. NET does not provide a ready-made way to use the hack level code.

3, error page

(1, when the page error occurs, ASP. NET will display the error message, this is not good-looking, and will reveal the site's internal implementation information, to the site to bring security risks, so you need to customize the error page, when the error shows the developer customized page. 404 page to put some ads is also good.
Configure Web.config to configure the customerrors area within system.web:

Copy Code code as follows:

<customerrors mode= "on" defaultredirect= "~myerrorpage.aspx" >
<error statuscode= "403" redirect= "~/noaccess.htm"/>
<error statuscode= "404" redirect= "~/filenotfound.htm"/>
</customErrors>

**mode Three optional values: On: Always show custom error pages; off: Do not display custom error interface, display the exception information such as Call stack directly;

**remoteonly: For native access to display exception information such as Call stack, custom error page for external user display .

Generally set to RemoteOnly, this error occurs, the administrator can look at the server's browser detailed error information, ordinary users do not see.

When you learn the demo, mode is set to ON, otherwise you won't see the custom page. You can judge request.userhostaddress in the definition error page to set some IP to see the exception information, you can read the session if the administrator can see the exception information.

(2,error child element settings for different status codes using different error pages, many sites make 404 a special error page.) A status code error that is not set individually displays the page specified in defaultredirect.
The error page can use the HTM page, or you can use the ASPX page. You can get the exception object with HttpContext.Current.Server.GetLastError () in the ASPX page. In general, do not display exception information to the user, but use the later log4net and so on to record the exception to the system log.

* * * If you want to get the exception object in the error page, such as customerrors, set redirectmode= "Responserewrite"because the default is client redirection (redirectmode= " Responseredirect "), you can't get the exception object in the error page. *****

Copy Code code as follows:

<customerrors mode= "on" redirectmode= "Responserewrite" defaultredirect= "~myerrorpage.aspx" >
<error statuscode= "403" redirect= "~/noaccess.htm"/>
<error statuscode= "404" redirect= "~/filenotfound.htm"/>
</customErrors>

Related Article

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.