Saturday 30 May 2015

Stop Caching of Page from Browser in Asp.Net

if you want open page every time from server then stop browser caching.

for example after logout from application, 
back/forward button of browser may be display your confidential page,
so stop caching of these pages by writing single line in C# 
in page_load or any other method.

Response.Cache.SetNoStore();

OR

Response.CacheControl = "private, no-store";

Create Custom Configuration Config Section in Web.Config/ App.Config

Suppose i have explained example with Browser Compatibility Configuration with Custom config section.

Register Configuration Class into Config Section in Config File.

<configSections>
    <section name="BrowserConfiguration" type="Demo.Web.BrowserConfiguration, Demo" allowDefinition="Everywhere" allowLocation="true"/>
 </configSections>

Add Your Configuration below Config Section Tag into Web.Config/App.Config

<BrowserConfiguration>
    <platforms>
      <platform name="winnt">
        <browsers>
          <browser browsername="IE" version="10"></browser>
          <browser browsername="FIREFOX" version="9"></browser>
          <browser browsername="CHROME" version="25"></browser>
        </browsers>
      </platform>
      <platform name="mac">
        <browsers>
          <browser browsername="IE" version="8"></browser>
          <browser browsername="CHROME" version="20"></browser>
        </browsers>
      </platform>
      <platform name="mobile">
        <browsers>
          <browser browsername="IE" version="4"></browser>
          <browser browsername="CHROME" version="10"></browser>
        </browsers>
      </platform>
    </platforms>
  </BrowserConfiguration>

Create Class For Configuration as Below.



public class BrowserElement : ConfigurationElement
{
    [ConfigurationProperty("browsername", IsRequired = true, IsKey = true)]
    public string BrowserName
    {
        get
        {
            return this["browsername"] as string;
        }
    }


    [ConfigurationProperty("version", IsRequired = true, DefaultValue = 1.0)]
    public double Version
    {
        get
        {
            return Convert.ToDouble(this["version"]);
        }
    }
}

public class BrowserCollection : ConfigurationElementCollection
{
    public new BrowserElement this[string name]
    {
        get
        {
            if (IndexOf(name) < 0)
                return null;

            return (BrowserElement)BaseGet(name);
        }
    }

    public BrowserElement this[int index]
    {
        get
        {
            return (BrowserElement)BaseGet(index);
        }
    }


    public int IndexOf(string name)
    {
        for (int idx = 0; idx < base.Count; idx++)
        {
            if (this[idx].BrowserName.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                return idx;
        }

        return -1;
    }

    public int IndexOf(BrowserElement browser)
    {
        return BaseIndexOf(browser);
    }


    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new BrowserElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((BrowserElement)element).BrowserName;
    }

    protected override string ElementName
    {
        get { return "browser"; }
    }
}

public class PlatformElement : ConfigurationElement
{
    private string m_platform = "winnt";

    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get
        {
            return Convert.ToString(base["name"]);
        }
    }

    [ConfigurationProperty("browsers", IsDefaultCollection = false)]
    public BrowserCollection Browsers
    {
        get
        {
            return (BrowserCollection)base["browsers"];
        }
    }
}

public class PlatformCollection : ConfigurationElementCollection
{
    new public PlatformElement this[string name]
    {
        get
        {
            if (IndexOf(name) < 0)
                return null;

            return (PlatformElement)BaseGet(name);
        }
    }

    public PlatformElement this[int index]
    {
        get
        {
            return (PlatformElement)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }

            BaseAdd(index, value);
        }
    }


    public int IndexOf(string name)
    {
        for (int idx = 0; idx < base.Count; idx++)
        {
            if (this[idx].Name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase))
                return idx;
        }

        return -1;
    }

    public int IndexOf(PlatformElement platform)
    {
        return BaseIndexOf(platform);
    }


    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new PlatformElement();
    }

    protected override Object GetElementKey(ConfigurationElement element)
    {
        return ((PlatformElement)element).Name;
    }


    public void Add(PlatformElement platform)
    {
        BaseAdd(platform);
    }

    protected override void BaseAdd(ConfigurationElement element)
    {
        BaseAdd(element, false);
    }

    public void Remove(PlatformElement platform)
    {
        if (BaseIndexOf(platform) >= 0)
            BaseRemove(platform.Name);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }

    public void Clear()
    {
        BaseClear();
    }

    protected override string ElementName
    {
        get { return "platform"; }
    }
}

public class BrowserConfiguration : ConfigurationSection
{
    private static string m_ConfigurationSectionConst = "BrowserConfiguration";

    public static BrowserConfiguration GetConfig()
    {
        return (BrowserConfiguration)ConfigurationManager.GetSection(BrowserConfiguration.m_ConfigurationSectionConst) ?? new BrowserConfiguration();
    }


    [ConfigurationCollection(typeof(PlatformCollection), AddItemName = "platform")]
    [ConfigurationProperty("platforms", IsDefaultCollection = true)]
    public PlatformCollection Platforms
    {
        get
        {
            return (PlatformCollection)this["platforms"] ?? new PlatformCollection();
        }
    }
}


To read Config Section Configuration into Code , Write below Code 



var collection = BrowserConfiguration.GetConfig();

PlatformCollection values = collection.Platforms;

int index = values.IndexOf(Request.Browser.Platform);

if (index >= 0)
{
    PlatformElement platform = values[index];

    int browserindex = platform.Browsers.IndexOf(Request.Browser.Browser);

    if (browserindex >= 0)
    {
        BrowserElement browser = platform.Browsers[browserindex];

        if (browser.Version > (Convert.ToDouble(Request.Browser.MajorVersion) + Request.Browser.MinorVersion))
        {
            Server.Transfer("BrowserCompatible.aspx", false);
        }
    }
}