ASP网站制作之Caching in ASP.NET
asp可以使用微软的activeX使得网页功能无比强大,不过安全性也较差,而且是基于的windows服务器,所以性能稳定性也一般asp.netThemajorityofthepagesinadynamicwebsitearedynamic.Thatis,pagesthatarecreatedonuserrequest.Asweallknow,dynamicwebpageshelptoprovidedynamiccontent,customizedfortheuserrequestingthepage.Dynamicpagesalsohelpprovidedynamiccontentfetchedfromachangingdatastorewithouttheneedfortheadministratortochangethepagecontenteverytimesomethingchangesinthedatastore.Thedisadvantageistheoverheadincreatingthepagesforeveryuserrequest.Toovercomethis,somewebsiteshavepagecreationengineswhichcreateallpagesinonegoandsavethemasHTMLpageswhicharethenservedtotheusers.Butthiswillonlyhelpinscenarioswherethepagecontentisthesameforallrequestsasinthesecondexampleabove.Thelistingofbooksisthesameirrespectiveoftheuserrequestingthepage.EvenifthereisprovisionforlistingbookscategorywisebyprovidingdifferentcategoryIDvaluesthroughthequerystring,thepageoutputforaparticularcategoryofbooksisthesameforallusers.
ASP.NETprovidessupportfor"caching"whichwillhelpussolvethisproblemtoagreatextend.Itcancachetheoutputgeneratedbyapageandwillservethiscachedcontentforfuturerequests.Andthisisusefulonlyinthesecondscenariodescribedearlier,wherethepagecontentisthesameforallrequests.Thecachingfeatureiscustomizableinvariouswaysandwewillseehowwecandothataswegothroughthisarticle.
Cachingapage
Inordertocacheapagesoutput,weneedtospecifyan@OutputCachedirectiveatthetopofthepage.Thesyntaxisasshownbelow:
<%@OutputCacheDuration=5VaryByParam="None"%>
Asyoucansee,therearetwoattributestothisdirective.Theyare:
Duration-Thetimeinsecondsofhowlongtheoutputshouldbecached.Afterthespecifieddurationhaselapsed,thecachedoutputwillberemovedandpagecontentgeneratedforthenextrequest.Thatoutputwillagainbecachedfor10secondsandtheprocessrepeats.
VaryByParam-Thisattributeiscompulsoryandspecifiesthequerystringparameterstovarythecache.
Intheabovesnippet,wehavespecifiedtheVaryByParamattributeasNonewhichmeansthepagecontenttobeservedisthesameregardlessoftheparameterspassedthroughthequerystring.
Iftherearetworequeststothesamepagewithvaryingquerystringparameters,e.g.:.../PageCachingByParam.aspx?id=12and.../PageCachingByParam.aspx?id=15]andseparatepagecontentisgeneratedforeachofthem,thedirectiveshouldbe:
<%@OutputCacheDuration=5VaryByParam="id"%>
ThepagecontentforthetworequestswilleachbecachedforthetimespecifiedbytheDurationattribute.
Tospecifymultipleparameters,usesemicolontoseparatetheparameternames.IfwespecifytheVaryByParamattributeas*,thecachedcontentisvariedforallparameterspassedthroughthequerystring.
Somepagesgeneratedifferentcontentfordifferentbrowsers.Insuchcases,thereisprovisiontovarythecachedoutputfordifferentbrowsers.The@OutputCachedirectivehastobemodifiedto:
<%@OutputCacheDuration=5VaryByParam="id"VaryByCustom="browser"%>
Thiswillvarythecachedoutputnotonlyforthebrowserbutalsoitsmajorversion.I.e.,IE5,IE6,Netscape4,Netscape6willallgetdifferentcachedversionsoftheoutput.
Cachingpagefragments
Sometimeswemightwanttocachejustportionsofapage.Forexample,wemighthaveaheaderforourpagewhichwillhavethesamecontentforallusers.Theremightbesometext/imageintheheaderwhichmightchangeeveryday.Inthatcase,wewillwanttocachethisheaderforadurationofaday.
Thesolutionistoputtheheadercontentsintoausercontrolandthenspecifythattheusercontrolcontentshouldbecached.Thistechniqueiscalledfragmentcaching.
Tospecifythatausercontrolshouldbecached,weusethe@OutputCachedirectivejustlikeweuseditforthepage.
<%@OutputCacheDuration=10VaryByParam="None"%>
Withtheabovedirective,theusercontrolcontentwillbecachedforthetimespecifiedbytheDurationattribute.Regardlessofthequerystringparametersandbrowsertypeand/orversion,thesamecachedoutputisserved..
DataCaching
ASP.NETalsosupportscachingofdataasobject</p>asp可以使用微软的activeX使得网页功能无比强大,不过安全性也较差,而且是基于的windows服务器,所以性能稳定性也一般 Request:从字面上讲就是“请求”,因此这个是处理客户端提交的东东的,例如Resuest.Form,Request.QueryString,或者干脆Request("变量名") 封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变,业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高。 如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助: 我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。 代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。 不能只是将它停留在纸上谈兵的程度上。 我想问如何掌握学习节奏(先学什么再学什么)最好详细点? 多看多学多思。多看一些关于ASP的书籍,一方面可以扩展知识面一方面可以鉴借别人是如何掌握、运用ASP的;多学善于关注别人,向同学老师多多学习,不论知识的大小;多思则是要将学到的知识灵活运用。 它可通过内置的组件实现更强大的功能,如使用A-DO可以轻松地访问数据库。 我就感觉到ASP和一些常用的数据库编程以及软件工程方面的思想是非常重要的。我现在也在尝试自己做网页,这其中就用到了ASP,我想它的作用是可想而知的。 ASP.Net摆脱了以前ASP使用脚本语言来编程的缺点,理论上可以使用任何编程语言包括C++,VB,JS等等,当然,最合适的编程语言还是MS为.NetFrmaework专门推出的C(读csharp),它可以看作是VC和Java的混合体吧。
页:
[1]