Thursday, December 25, 2008

Disable browser caching in asp.net

To improve speed and response to user, some browsers cache the whole webpage. when user request for same page, instead of going to server, browsers server this page from the cache.
This approach can create problem, browser caches a secured page, which user should see only if he is login. There is a way to force the browser not to cache an ASP.NET web page and you need to add only 1 line of code to your ASP.NET web pages in order to disable browser caching. 

Add the following line on top of the Page_Load event handler and your ASP.NET page will not be cached in the users browsers: 

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Caching in asp.net

One of the techniques developers commonly relied on to speed up processing of request is the use of caching. When information is cached, it stays cached either indefinitely, until some relative time, or until some absolute time. Most commonly, information is cached for a relative time frame. That is, our database information may be fairly static, updated just a few times a week. Therefore, we might want to invalidate the cache every other day, meaning every other day the cached content is rebuilt from the database.

There are a number of classes in the .NET Framework designed to aid with caching information. ASP.NET supports three types of caching for Web-based applications:
  • Page Level Caching (called Output Caching)
  • Page Fragment Caching (often called Partial-Page Output Caching)
Output Caching : 
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. Output caching is easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:

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

The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache. 
The VaryByParam is a useful setting that can be used to cache different "views" of a dynamic page whose content is generated by GET or POST values. For example, you may have an ASP.NET Web page that reads in a Part number from the QueryString and displays information about a particular widget whose part number matches the QueryString Part number. Imagine for a moment that Output Caching ignored the QueryString parameters altogether (which you can do by setting VaryByParam="none"). If the first user visited the page with QueryString /ProductInfo.aspx?PartNo=4, she would see information out widget #4. The HTML for this page would be cached. The next user now visits and wished to see information on widget #8, a la /ProductInfo.aspx?PartNo=8. If VaryByParam is set to VaryByParam="none", the Output Caching engine will assume that the requests to the two pages are synonymous, and return the cached HTML for widget #4 to the person wishing to see widget #8! To solve for this problem, you can specify that the Output Caching engine should vary its caches based on the PartNo parameter by either specifying it explicitly, like VaryByParam="PartNo", or by saying to vary on all GET/POST parameters, like: VaryByParam="*".

Partial-Page Output Caching:
Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached. this can be done by caching the usercontrols.  This caching can be done by adding an OutputCache directive at the top of the User Control.


Tuesday, December 9, 2008

Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true

If you get this error when calling web service asynchronously, add the Async=”true” attribute in the page directive at the top of page.