[Go] Implement ASP. NET MVC 3 page static using Resultfilter

Source: Internet
Author: User

In order to improve the performance of the website, and the load capacity of the site, page static is an effective way, here for the Web site under the MVC3 framework, provide a kind of static speech method that the person thinks relatively well.

The implementation principle is to implement the text saving of the page content through the filter extension point provided by MVC, directly on the code:

1  Public classStaticfilewritefilterattribute:filterattribute, Iresultfilter2     {3          Public voidonresultexecuted (resultexecutedcontext filtercontext)4         {5 6         }7 8          Public voidonresultexecuting (resultexecutingcontext filtercontext)9         {TenFilterContext.HttpContext.Response.Filter =NewStaticfilewriteresponsefilterwrapper (FilterContext.HttpContext.Response.Filter, filtercontext); One         } A  -         classStaticFileWriteResponseFilterWrapper:System.IO.Stream -         { the             PrivateSystem.IO.Stream inner; -             PrivateControllerContext context; -              PublicStaticfilewriteresponsefilterwrapper (System.IO.Stream s,controllercontext context) -             { +                  This. Inner =s; -                  This. Context =context; +             } A  at              Public Override BOOLCanRead -             { -                 Get{returnInner. CanRead; } -             } -  -              Public Override BOOLCanSeek in             { -                 Get{returnInner. CanSeek; } to             } +  -              Public Override BOOLCanWrite the             { *                 Get{returnInner. CanWrite; } $             }Panax Notoginseng  -              Public Override voidFlush () the             { + Inner. Flush (); A             } the  +              Public Override LongLength -             { $                 Get{returnInner. Length; } $             } -  -              Public Override LongPosition the             { -                 GetWuyi                 { the                     returnInner. Position; -                 } Wu                 Set -                 { AboutInner. Position =value; $                 } -             } -  -              Public Override intRead (byte[] Buffer,intOffsetintcount) A             { +                 returnInner. Read (buffer, offset, count); the             } -  $              Public Override LongSeek (Longoffset, System.IO.SeekOrigin origin) the             { the                 returnInner. Seek (offset, origin); the             } the  -              Public Override voidSetLength (Longvalue) in             { the Inner. SetLength (value); the             } About  the              Public Override voidWrite (byte[] Buffer,intOffsetintcount) the             { the Inner. Write (buffer, offset, count); +                 Try -                 { the                     stringp =context. HttpContext.Server.MapPath (HttpContext.Current.Request.Path);Bayi  the                     if(Path.hasextension (p)) the                     { -                         stringDIR =Path.getdirectoryname (p); -                         if(!directory.exists (dir)) the                         { the Directory.CreateDirectory (dir); the                         } the                         if(File.exists (p)) -                         { the File.delete (p); the                         } the File.appendalltext (p, System.Text.Encoding.UTF8.GetString (buffer));94                     } the                 } the                 Catch the                 { 98                  About                 } -             }101         }102}
View Code

Our class Staticfilewritefilterattribute implements the Iresultfilter, this interface has two methods, onresultexecuted and Onresultexecuting, where Onresultexecuting is executed after the action code in the controller executes, but before Viewresult executes (that is, before the page content is generated), the Onresultexecuted method is Viewresult Executes after execution (that is, after the page content is generated).

We register the Staticfilewriteresponsefilterwrapper class with the Filter property of the Response object before the page is generated, where the wrapper class can inject the static code of the page content without any side effects. The action that handles the business logic is transparent.

[Go] Implement ASP. NET MVC 3 page static using Resultfilter

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.