Use of viewstate and cache in ASP.NET notes

  • 2020-06-01 09:35:01
  • OfStack

1.

(1) by default, ASP.Net is enabled with ViewState, which will generate lengthy hidden fields in the page. ViewState may be useful for pages that need to be processed by PostBack.

(2) ways to disable ViewState:
The & # 8226; ViewState disabled as a whole: EnableViewState="False" in Page at the top
The & # 8226; Specify control to disable ViewState, on control EnableViewState="False"

(3) when ViewState is disabled, ViewState is not completely removed, as long as ViewState is not too big. If a little bit of ViewState is not required, then form with runat=server cannot be found on the page. If there are no form elements on the page, form can be completely removed. Server controls such as Button are not available if they are not placed in form where runat=server.

2, cache

(1) if you query the database to generate page content every time you enter the page, if the traffic is very large, the performance of the website will be very poor. If you only query the database to generate page content on the first visit, and then output the content directly, you can improve the system performance. In this way, no matter how many people visit the database, they only visit the database once and the database pressure remains the same.

Cache is a technology that trades space for time. It exists in many places in the computer. It is used to save the common data in some slow devices in the fast device, and to fetch the data directly from the fast device. For example, CPU level 2 cache, windows file read cache.

Cache invalidation is a problem: in order to ensure that the data read from the cache is equal to the data 1 in the slow data, it is necessary to clear the corresponding data in the cache when the corresponding data in the slow data changes.
Caching is the first way to improve web site performance, just as indexing is the first way to improve database performance.
ASP. net cache is mainly divided into three types: page cache, data source cache and data cache.

(2) page caching

You can enable page caching by adding the following tags to the page,


<%@ OutputCache Duration="20"  VaryByParam="none"%>

The ASP.Net code and data source in the page will not be run during the cache, but will output the cached page content directly. Duration represents the cache time, measured in seconds, after which the cache will be invalidated, regenerated and cached for another 20 seconds, and so on. Set a breakpoint at Page_Load and modify the database data test.
The cache is for all visitors to the page. So one visitor and 10,000 visitors, one visit and a million visits put the same amount of pressure on the database.

*** for viewing news pages, if set as above, the first news you see will be cached, because? id = 2,? id=3 is just the different parameters of the page. In order to enable different news to be cached separately, VaryByParam="id" can be set, which means that different id parameters can be cached separately. 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 different query string, set VaryByParam="*", and in 1 case setting "*" is sufficient.
You can also set the control's cache in WebUserControl, just like page cache 1.

(3) data source cache
Set CacheDuration (cache time: seconds) of ObjectDataSource, EnableCaching=true. In this way, the method specified by SelectMethod is called every other time period specified by CacheDuration to execute the database query, and the rest of the time the cached data is returned directly.

Cache fixed time is suitable for the home page, list of articles, such as access frequently page, page for see stick is not suitable for, suppose you have 1 million posts, if every post is fixed cache for 1 hour, assuming 100000 posts have been looked at 1 hour, 100000 posts, then cache memory very much, because of the "one hundred" "grave" and then be accessed once cache also 1 hours, memory. You can use the "sliding window (sliding)" strategy, such as caching a post for 10 minutes. If the post is accessed again within 10 minutes, the cache expiration time will be changed to 10 minutes after the moment it was accessed, and so on. This allows frequently accessed posts to be "cached for a long time," while infrequently accessed posts won't be cached for a long time due to occasional visits. Set method, data source: CacheExpirationPolicy="Sliding". An interview is something to talk about. todo: there seems to be a problem with sliding. Not a problem, Sliding is just a policy that the server will refer to.

(4) cache others
Page cache, data source cache and other internal use of HttpRuntime.Cache to achieve caching, in 1 page cache, data source cache can not complete the special cache requirements, you can directly call HttpRuntime.Cache for caching.

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

3. Error page

(1) when a page error occurs, ASP.Net will display the error information, so 1 will not look good, 2 will reveal the internal implementation information of the website, bringing security risks to the website, so it is necessary to customize the error page, and display the page customized by the developer when the error occurs. It would be nice to have ads on a 404 page.
Configure web.config, configure customErrors area within system.web:


<customErrors mode="On" defaultRedirect="~MyErrorPage.aspx">
 <error statusCode="403" redirect="~/NoAccess.htm" />
 <error statusCode="404" redirect="~/FileNotFound.htm" />
 </customErrors>

**mode3 optional values: On: always display custom error pages; Off: does not display the custom error interface, directly displays the call stack and other abnormal information;

**remoteonly: access to the native displays exception information such as the call stack, and displays custom error pages for external users.

1 general Settings RemoteOnly, so that if an error occurs, the administrator can see the detailed error information in the browser of the server, ordinary users can not see.

When learning the demo, mode is set to On, otherwise you will not see the custom page. You can set some ip to see the exception message by judging Request.UserHostAddress in the definition error page, and you can read Session to see the exception message if you are an administrator.

(2) the error child element is configured to use different error pages for different status codes. Many websites make 404 a special error page. A status code error that is not set separately displays the page specified in defaultRedirect.
The error page can use either the htm page or the aspx page. Can be used in the aspx page HttpContext. Current. Server. GetLastError () to get the exception object. 1 generally do not display the exception information to the user, but record the exception to the system log using Log4Net as described below.

***** if you want to get the exception object in the error page, for example, redirectMode="ResponseRewrite" is set in customErrors, because the default is client redirection (redirectMode="ResponseRedirect"), you will not get the exception object in the error page. * * * * *


<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~MyErrorPage.aspx">
<error statusCode="403" redirect="~/NoAccess.htm" />
<error statusCode="404" redirect="~/FileNotFound.htm" />
</customErrors>


Related articles: