Passing Context – Accessing Context from Outside the Page Class

Passing Context – HttpContext Class के मुख्‍य रूप से दो Features होते हैं, जो कि ASP.NET का अन्तिम Context Related Feature है:

  • ये हमें सभी अन्‍य Context Objects को अपने Logic Codes में कहीं पर भी Access करने की सुविधा Provide करता है।
  • ये हमें विभिन्न Pages के बीच Information को Pass करने का एक Alternative तरीका Provide करता है।

Accessing Context from Outside the Page Class

जब हम किसी Class को Derive कर रहे होते हैं, तब उस Class में विभिन्न Context Objects को Page Class की Properties के माध्‍यम से Access करना एक Perfect तरीका होता है। लेकिन जब हम Class को Derive नहीं कर रहे होते हैं, तब उस Class में विभिन्न Context Objects को Access करना आसान नहीं होता।

क्योंकि जब हम Context Information को किसी अन्‍य Class में Access करना चाहते हैं, तब हमारे पास दो Options होते हैं:

  • पहले Option के अन्तर्गत हम सभी जरूरी Information को Query Parameter के रूप में Pass करना होता है, जैसाकि पिछले Section में हमने Discuss किया था।
  • जबकि दूसरे Option के अन्तर्गत हम HttpContext Class की Current नाम की Static Property कोUse कर सकते हैं और Current Page Request से सम्बंधित सभी Context Objects को HttpContext Object के माध्‍यम से Access कर सकते हैं।

इस दूसरे तरीके को समझने के लिए हम हमारी Default.aspx.cs Code Behind File को निम्नानुसार Modify कर सकते हैं, जिसके अन्तर्गत हम Default.aspx Page Class में किसी OtherClass के Contexts को Use कर रहे हैं, जिसका Context, Default Class के Context से भिन्न है:

[code]
File Name: Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventsAndContexts
{
  public partial class Default : System.Web.UI.Page
  {
    protectedvoid Page_Load(object sender, EventArgs e)
    {
      //Creating Object of OtherClass
      OtherClass ocObject = newOtherClass();

      //Calling Method of OtherClass
      ocObject.TransferBasedOnBrowser();
    }
  }

  class OtherClass
  {
    public void TransferBasedOnBrowser()
    {
      //Get the HttpContext Object
      HttpContext actualContext = HttpContext.Current;
      switch (actualContext.Request.Browser.Browser)
      {
        case "Chrome":
          actualContext.Server.Transfer("SecondPage.aspx?msg=firstMessage");
        break;
        case "IE":
          actualContext.Server.Transfer("SecondPage.aspx?msg=secondMessage");
        break;
        default:
          actualContext.Server.Transfer("SecondPage.aspx");
        break;
      }
    }
  }
}
[/code]

जब हम Default.aspx.cs Code Behind File को उपरोक्तानुसार Code द्वारा Modify करते हैं, तब भी हमें प्राप्त होने वाला Output Exactly पिछले Section में Create किए गए Example Program की तरह ही प्राप्त होता है, जो कि Use किए जाने वाले Web Browser के आधार पर Message Render करता है। लेकिन इस बार ये Output मुख्‍य Code Behind Class द्वारा Generate नहीं हो रहा है, बल्कि HttpContext Object के माध्‍यम से Generate हो रहा है।

इस HttpContext Class को Use करने की जरूरत इसलिए है, क्योंकि समान Namespace की सभी Classes हालांकि एक दूसरे के लिए Directly Accessible होती हैं, लेकिन एक Class सभी Context Object केवल उसी Class के लिए Directly Available रहते हैं, अन्‍य Class के लिए नहीं।

इसीलिए किसी User Request को Process करने के लिए Page Class से Derive की गई Default Class में तो Request, Response, Server आदि सभी Context Objects Directly Available व Accessible रहते हैं, लेकिन OtherClass को Default Class के किसी भी Context Object के बारे में कोई जानकारी नहीं होती, बल्कि OtherClass का स्वयं का अपना अलग Class Scope होता है और उस Scope में Current User Request की Processing से सम्बंधित कोई भी Context Object Exist नहीं होता।

अत: जब हम Default Class में OtherClass का Object Create करके OtherClass के Method को Invoke किया जाता है, तब Default Class के Context Objects, OtherClass के ocObjectमें Available नहीं होते।

परिणामस्वरूप जब OtherClass का TransferBasedOnBrowser() Method Execute होता है, तब उसके लिए Request, ResponseServer Context Objects Available ही नहीं होते। क्योंकि ये Context Objects केवल उस Derived Class में ही Available होते हैं, जिसे Page Class से Derive किया गया हो।

इसलिए इस OtherClass के TransferBasedOnBrowser() Method के अन्तर्गत हम निम्नानुसार तरीके से Server.Transfer() Method को Directly Invoke ही नहीं कर सकते-

[code]
    public void TransferBasedOnBrowser()
    {
      switch (Request.Browser.Browser)
      {
        case"Chrome":
          Server.Transfer("SecondPage.aspx?msg=firstMessage");
        break;
        case"IE":
          Server.Transfer("SecondPage.aspx?msg=secondMessage");
        break;
        default:
          Server.Transfer("SecondPage.aspx");
        break;
      }
    }
[/code]

लेकिन जब हम OtherClass जैसी किसी Class में निम्नानुसार Statement के माध्‍यम से actualContext नाम का Object Create कर देते हैं:

//Get the HttpContext Object

HttpContext actualContext = HttpContext.Current;

तब इस OtherClass का Object जिस Class में Create करते हैं, उस Class के विभिन्न Context Objects को HttpContext.Current Property द्वारा actualContext नाम के Object के माध्‍यम से Refer कर सकते हैं।

चूंकि हमारे उदाहरण में हमने Default Class में OtherClass काocObject Create किया है, इसलिए इस ocObject के Create होते ही Default Class के सभी Context Objects का Reference actualContext नाम के Object में Initialize हो जाता है।

परिणामस्वरूप इस OtherClass विभिन्न Methods में हम Default Class के सभी Context Objects को Refer करने के लिए actualContext नाम के Reference Object को Use कर सकते हैं और इसीलिए हमने हमारी OtherClass केTransferBasedOnBrowser() Method के Logic Codes को निम्नानुसार तरीके से actualContext Reference Variable का प्रयोग करते हुए लिखा है:

[code]
    public void TransferBasedOnBrowser()
    {
      //Get the HttpContext Object
      HttpContext actualContext = HttpContext.Current;
      switch (actualContext.Request.Browser.Browser)
      {
        case"Chrome":
          actualContext.Server.Transfer("SecondPage.aspx?msg=firstMessage");
        break;
        case"IE":
          actualContext.Server.Transfer("SecondPage.aspx?msg=secondMessage");
        break;
        default:
          actualContext.Server.Transfer("SecondPage.aspx");
        break;
      }
    }
[/code]

सरल शब्दों में कहें तो जिस Class में हम Page Class द्वारा Provide किए जाने वाले विभिन्न Context Objects (Request, Response, Server, etc…) को प्राप्त करना चाहते हैं, उस Class में हमें निम्नानुसार तरीके से HttpContext Type का Reference Create करना होता है:

HttpContext actualContext = HttpContext.Current;

परिणामस्वरूप जिस Target Class (OtherClass) में हम ये Statement लिख देते हैं, उस Target Class (OtherClass) के सभी Methods के लिए उसे Source Class (Default) के सभी Context Objects (Request, Response, Server) Available हो जाते हैं, जिसमें इस Target Class (OtherClass) का Object Create किया जाता है।

अन्‍य शब्दों में कहें तो उपरोक्त Example का TransferBasedOnBrowser() Method, User Request व Server Context Object पर निर्भर है। OtherClass में Retrieve करने के लिए HttpContext.Current Property को Use किया गया है, जो कि Current Class के Context को Refer करता है और इसी के माध्‍यम से इस Method में Request व Server Properties को Read किया गया है। ये HttpContext.Current Properly, HttpRequest व HttpServerUtility Classes के Objects Return करता है, जो कि Current Page Request से सम्बंधित होते हैं।

परिणामस्वरूप हम इस HttpContext.Current Property के माध्‍यम से Returned Reference द्वारा Browser Type Detect कर सकते हैं और Particular Browser Typeके अनुसार Request को Transfer कर सकते हैं, जबकि इस Transfer के लिए हमें Context Object या Query Parameter के रूप में Method Parameters को Pass करने की जरूरत नहीं होती, जैसाकि पिछले उदाहरण में थी।

HttpContext Class में प्रत्येक Context Object Contained होता है और उन सभी में Page Class की Properties के Equivalent Properties Exist होती हैं। इसलिए OtherClass में हम Request Property के माध्‍यम से HttpRequest Object को Access कर सकते हैं, Response Property के माध्‍यम से HttpResponse Object को Access कर सकते हैं, आदि। जबकि ये Objects मूल रूप से केवल Page Class की Derived Class में ही Exist होते हैं।

एक बात और ध्‍यान रखनी चाहिए कि जब भी User नया Request Perform करता है, एक नया Request Object Create होता है, इसलिए किसी पुराने Request Object को Save करके नहीं रखना चाहिए और न ही पिछली Request के Data को Access करने की कोशिश करनी चाहिए।

Read more …

Passing Information using Items Collection

Core ASP.NET WebForms in Hindi - BccFalna.com: TechTalks in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Core ASP.NET WebForms with C# in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

Core ASP.NET WebForms in Hindi | Page:647 | Format: PDF

BUY NOW GET DEMO REVIEWS