Data caching in ASP.Net

Data caching in ASP.Net can be done directly via Page.Cache or HttpContext.Current.Cache. In here you can store any kind of object globally, like a file or some data retrieved from a lengthy query to the database. You can use this data on different pages and with different users as long as the cache is valid. More…

OutputCache for Specific Browsers in ASP.Net

To enable page output caching in ASP.Net for specific browser types, simply call something like…

using System;
using System.Web;

namespace WebCache
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // A five second global page cache for all users
            // but specific for different types of browsers
            Response.Cache.SetCacheability(HttpCacheability.Public);
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(5));
            Response.Cache.SetVaryByCustom("Browser");

            // Show the cached time
            labelTimeStamp.Text = DateTime.Now.ToLongTimeString();
        }
    }
}

Or, if you prefer declarative in the aspx page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
    AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="WebCache._Default" %>
<%@ OutputCache Duration="5" VaryByCustom="Browser" VaryByParam="None" %>  More...

Recreate tables when a Code First class changes

If you want to automatically recreate the database if your Code First model changes in an asp.net application, add the following code to the Application_Start in global.asax.cs. Call Database.SetInitializer for your DbContext class.

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Database.SetInitializer<TwitterDbContext>(
        new DropCreateDatabaseIfModelChanges<TwitterDbContext>());
}  More...