Asp.net MVC (10)

Source: Internet
Author: User

This articleArticleI will discuss two basic usage of page optimization. Next I will analyze the usage of Static Page cache. Although the Internet environment is much better, the increasingly fluent and fat client of websites makes page loading not much faster, so how to increase the response speed has become a great place for everyone.

First: outputcacheattribute, This page-level cache I want to use for web form developmentProgramIt can cache the entire page and support multiple parameter formats and expiration policies. In the preview version before Asp.net MVC 1.0, it seems that this is not found. There are too many preview versions. If there is any inaccuracy, please forgive me. outputcacheattribute is added to the framework after it reaches 1.0. Let's take a look at its source code, mainly the onresultexecuting method. The Duration Attribute indicates the expiration time, And the varybyparam attribute indicates whether the cache is related to the parameter.

Code

[Attributeusage (attributetargets. Method | Attributetargets. Class, inherited = True , Allowmultiple = False )]
Public   Class Outputcacheattribute: actionfilterattribute
{
// Fields
Private Outputcacheparameters _ cachesettings;

//Methods
PublicOutputcacheattribute ();
Public Override VoidOnresultexecuting (resultexecutingcontext filtercontext );

// Properties
Public   String Cacheprofile { Get ; Set ;}
Internal Outputcacheparameters cachesettings { Get ;}
Public   Int Duration { Get ; Set ;}
Public Outputcachelocation location { Get ; Set ;}
Public   Bool Nostore { Get ; Set ;}
Public   String Sqldependency { Get ; Set ;}
Public   String Varybycontentencoding { Get ; Set ;}
Public   String Varybycustom { Get ; Set ;}
Public   String Varybyheader { Get ; Set ;}
Public   String Varybyparam { Get ; Set ;}

// nested types
private sealed class outputcachedpage: page
{< br>
/// fields
private outputcacheparameters _ cachesettings;

//Methods
PublicOutputcachedpage (outputcacheparameters cachesettings );
Protected Override VoidFrameworkinitialize ();
}
}

Outputcacheattribute is easy to use:
1: directly write it in the controller, for example:
But this is not recommended because the cache parameters are written inCode.

[Outputcache (Duration =   10 , Varybyparam =   " None " )]
Public Actionresult index ()
{
Return View ();
}

2: You can write it on the page: <% @ outputcache duration = "10" varybyparam = "NONE" %>.

3: You can write it in the configuration file. Two steps are required.

1>: add the cache feature to the Controller code.

[Outputcache (cacheprofile =   " Myprofile " )]
Public Actionresult index ()

2>: the Web. config configuration is as follows: Name Is the cacheprofile in the Controller code.

Code

< Caching >
< Outputcachesettings >
< Outputcacheprofiles >
< Add name = " Myprofile " Duration = " 60 " Varybyparam = " * "   />
</ Outputcacheprofiles >
</ Outputcachesettings >
</ Caching >

 

Second, compressattribute.Generally, in addition to program optimization, large websites also have servers. One common method is to enable gzip compression. We can also implement it in the program, and it is not difficult. The principle is
Add gzipstream/deflatestream to response. Filter stream.

Code

Public   Class Compressattribute: actionfilterattribute
{
Public   Override   Void Onactionexecuting (actionexecutingcontext filtercontext)
{
VaR acceptencoding
= Filtercontext. httpcontext. Request. headers [ " Accept-Encoding " ];
If ( ! String . Isnullorempty (acceptencoding ))
{
Acceptencoding
= Acceptencoding. tolower ();
VaR response
= Filtercontext. httpcontext. response;

If (Acceptencoding. Contains ( " Gzip " ))
{
Response. appendheader (
" Content-Encoding " , " Gzip " );
Response. Filter
=   New Gzipstream (response. filter, compressionmode. Compress );
}
Else   If (Acceptencoding. Contains ( " Deflate " ))
{
Response. appendheader (
" Content-Encoding " , " Deflate " );
Response. Filter
=   New Deflatestream (response. filter, compressionmode. Compress );
}
}
}
}

 

Let's take a look at the effect before and after enabling compression: We use firebug to observe the page size.

This is the figure when compression is not enabled.

 

This is after compression is enabled. Although my page is relatively small, although I can't see the loading time problem, the page is obviously smaller, and the large page will have a more obvious effect on time loading.

 

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.