Use of viewstate and cache in ASP. NET notes

Source: Internet
Author: User
Tags website performance

1,

(1. ASP by default. net enables ViewState, which generates lengthy hidden fields on the page. ViewState may be useful for pages that require PostBack processing, viewState is not required for interaction on news display pages.

(2) Disable ViewState:
• Disable ViewState on the Page as a whole: EnableViewState = "False" on the top Page"
• Specify the control to disable ViewState and EnableViewState = "False" on the control"

(3. ViewState is not completely removed after ViewState is disabled on the page, as long as the ViewState is not very large. If no ViewState is required, the page cannot contain a form with runat = server. If there is no form Element in the page, you can remove the form completely. If buttons and other server-side controls are not placed in the form of runat = server, they are unavailable.

2. cache

(1,If you query the content on the page generated by the database every time you enter the page, the website performance will be very poor if the access volume is very large. However, if the content on the page generated by the database is queried only during the first access, the system performance will be improved if the content will be output directly in the future. In this way, no matter how many users access the database, the database pressure remains unchanged.

Cache is a technology that exchanges space for time. It exists in many places in a computer and is used to store frequently used data from slow devices on fast devices, you can retrieve data directly from the Quick device. For example, the CPU Level 2 Cache and windows File Read cache.

Cache failure: to ensure that the data read from the cache is consistent with the data in the slow data, when the corresponding data in the slow data changes, clear the corresponding data in the cache.
Cache is the first way to improve website performance, Just likeIndex is the first way to improve database performanceSame.
ASP.net cache is mainly divided into three types: Page cache, data source cache, and data cache.

(2) page Cache

Add the following labels to the page to enable page caching,

Copy codeThe Code is as follows: <% @ OutputCache Duration = "20" VaryByParam = "none" %>

In this way, the content of the entire page will be cached. ASP. Net code and data sources on the page will not be executed during the cache, but will directly output the cached page content. Duration indicates the cache time, in seconds. If this time is exceeded, the cache becomes invalid. After being generated again, the cache will be cached for another 20 seconds, and so on. Set a breakpoint at Page_Load and modify the database data for testing.
Cache is for all visitors to this page. In this way, the database pressure is the same for one visitor and 10 thousand visitors, one access and 1 million access.

***For the news page, if you set it above, it will cache the first news you see, because? Id = 2 ,? Id = 3 is only different parameters of the page. To enable different news to be cached separately, you can setVaryByParam = "id",Indicates separate caching of different id parameters..If multiple cached parameters exist, separate them with semicolons, for example, VaryByParam = "id; number ".
If you wantCreate different caches for any different query strings, SetVaryByParam = "*"In general, setting "*" is enough.
In WebUserControl, you can also set the control cache like the page cache.

(3) Data Source Cache
Set the CacheDuration of ObjectDataSource (Cache Time: seconds ),EnableCaching = true. So everyThe specified time range of CacheDuration.You can call the method specified by SelectMethod to query the database. In other cases, cache data is directly returned.

The fixed cache time applies to frequently accessed pages such as the home page and article list. It is not suitable for posting pages. Suppose there are 1 million posts. If each post is invariably cached for 1 hour, assume that 0.1 million posts have been read within an hour, so 100,000 posts need to be cached, which is very memory-consuming, because the "grave post" of "One Hundred Years of reading" was accidentally accessed and cached for an hour, it occupied memory. In this case, you can use the sliding policy. For example, if the post is cached for 10 minutes and accessed again within 10 minutes, then the cache expiration time is changed to 10 minutes from the moment the cache is accessed, and so on. In this way, frequently accessed posts can be "cached for a long time", and infrequently accessed posts will not occupy the cache for a long time due to accidental access. Setting method,Data source: CacheExpirationPolicy = "Sliding". Interview chat. Todo: it seems that there is a slide problem. No problem. Sliding is just a policy. The server will refer to it.

(4. cache other
HttpRuntime is used internally for page caching and Data Source caching. cache to implement Cache. In some special Cache requirements that cannot be completed by PAGE Cache and Data Source Cache, you can directly call HttpRuntime. cache.

(*) The ASP. Net cache is saved in memory by default and can be configured and saved to the database. Large websites also use Memcached and other technologies.
Clear cache. When the cache has not expired, you may need to immediately clear the cache so that the database changes are immediately reflected in the interface. ASP. Net does not provide a ready-made method. You can use Hack-level code.

3. Error Page

(1,When a page error occurs, ASP.. Net will display the error information, which is not easy to understand. Second, the internal implementation information of the website will be leaked, which brings security risks to the website. Therefore, you need to customize the error page, A custom page is displayed when an error occurs. 404 it is also good to place some advertisement on the page.
Configure web. config and configure the customErrors area in system. web:

Copy codeThe Code is as follows: <customErrors mode = "On" defaultRedirect = "~ MyErrorPage. aspx ">
<Error statusCode = "403" redirect = "~ /NoAccess.htm "/>
<Error statusCode = "404" redirect = "~ /FileNotFound.htm "/>
</CustomErrors>

** Three optional values of mode: On: the custom error page is always displayed; Off: the custom error page is not displayed.Directly displays exception information such as the call stack;

** Remoteonly: displays call stack and other exception information for local access and custom error pages for external users.

Usually set to RemoteOnly. In this case, the administrator canServer BrowserSee the detailed error message, which is invisible to common users.

Set mode to On when learning the demo. Otherwise, the custom page is not displayed. Can be determined on the definition error pageRequest. UserHostAddressTo set some ip addresses to see the exception information. You can read the Session and view the exception information if it is an administrator.

(2,The error sub-element sets different error pages for different status codes. Many websites make 404 a special error page. If no status code is set separately, the page specified in defaultRedirect is displayed.
The error page can be an htm page or An aspx page. You can useHttpContext. Current. Server. GetLastError ()Get the exception object. Generally, do not display the exception information to users. Instead, use the Log4Net mentioned later to record the exception to the system log.

* *** If you want to get the exception object on the error page, for example, set redirectMode = "ResponseRewrite" in customErrors"Because the default value is client redirection (redirectMode = "ResponseRedirect"), the exception object cannot be obtained on the error page.*****

Copy codeThe Code is 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.