29 Ağustos 2013 Perşembe

HttpApplication Class ile Cookie Örneği

Bu aralar her şeyi object oriented görmeye başladım.Sanırım matrixi yıkıyorum.

HttpApllication class ve cookie ile ilgili  minik bir oo örneği

Bu şekilde bir sınıf oluşturdum

namespace cookieControl
{
    public class cookieModul : IHttpModule
    {
        private HttpContext context;

        public cookieModul()
        {
       
        }

        public void Dispose()
        {
         
        }

        public void Init(HttpApplication app)
        {
            app.AcquireRequestState += new EventHandler(app_AcquireRequestState);
        }

        void app_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;
            this.context = HttpContext.Current;
        }
        public void set()
        {
            HttpCookie c = new HttpCookie("cookicik","değeri");
            HttpContext.Current.Response.Cookies.Add(c);

        }
    }
}


Daha sonra aspx sayfamda bu şeklide set ettim
  protected void Page_Load(object sender, EventArgs e)
        {
            new cookieModul().set();
        }

En sonda webconfig dosyasını güncelledim
     
           
     
   


Sonuçta:

Cookiecontrol sınfına IHttpMofule ınterfaceini impelement ettim.Böylece bu interfacedeki tüm metotdlara vs.. erişebildim.
Ardından önemli nokta AcquireRequestState  eventi.Bu event MSDN dediği gibi
"Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request."

yani

Asp.net'in durumu, ne zaman gelen isteğe cevap verirse bu metod çalışır.Yani bu eventi çalıştırmazsan context null olur.

Artık herkes cookilerini bir sınıf içinde kullanablir.Mutlu ve mesut insanlar.

İzleyiciler