Request Object or Request Context in ASP.NET

Request Object or Request Context in ASP.NET – जब कोई Web Application Run होता है, तो ASP.NET Current Application, Application के प्रत्येक User Session, Current HTTP Request, Requested Page आदि विभिन्न प्रकार की Information को Maintain रखता है और इन Context Information को Manage व Maintain करने के लिए ASP.NET Framework में कई Classes की एक Series Exist है।

हमें इन विभिन्न Classes से Instantiated Objects प्राप्त होते हैं, जिन्हें उपयोग में लेते हुए हम हमारे Web Application से सम्बंधित इन Contextual Information को Access व Manipulate कर सकते हैं। इन Classes द्वारा हमें मूल रूप से निम्नानुसार Context Objects प्राप्त होते हैं:

Request Object or Request Context

Request Context का प्रयोग करके हम User द्वारा Send की गई Request से सम्बंधित Information को Access करते हैं। यानी हम Current Page Request के Headers, Cookies, Client Certificate, Query String आदि की जानकारी प्राप्त करने के लिए इस Object को Use कर सकते हैं।

Perform की गई Request की Details प्राप्त करने के लिए हम Page.Request Property को Access कर सकते हैं, जो कि System.Web.HttpRequest Class का एक Object Return करता है।

इस Class को Demonstrate करने के लिए हम हमारे Example Project केDefault.aspx Page को निम्नानुसार Modify करते हुए एक Server Side में Access किया जा सकने वाला <div> Element Create कर सकते हैं:

[code]
File Name: Default.aspx
<%@Page
   Language="C#"
   AutoEventWireup="true"
    CodeBehind="Default.aspx.cs"
   Inherits="EventsAndContexts.Default"
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/x html">
<head runat="server">
<title></title>
</head>
<body>
<div id="contextDiv" runat="server"/>
</body>
</html>
[/code]

इस Webpage में हमने contextDiv id Attribute वाला एक नया<div> Element Specify किया है, जो कि Server Side में Manipulate हो सके, इसके लिए हमने इसके साथ  runat Attribute को भी Specify किया है। परिणामस्वरूप Parsing के दौरान Create होने वाली Temporary Class में हम इस Element को contextDiv  नाम के HTML Control के रूप में Access कर सकते हैं।

चूंकि Request Context या Object वास्तव में HttpRequest Class का Object होता है, जो कि हमें ASP.NET Framework द्वारा Automatically Provide किया जाता है।

HttpRequest Class में कई Properties व Methods को Define किया गया है, जिनके माध्‍यम से हम User द्वारा Perform की जाने वाली Request से सम्बंधित विभिन्न प्रकार की जानकारियों को Access कर सकते हैं। जैसे:

[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)
    {
      contextDiv.Innerhtml += (string.Format("<b>Application Path: </b>{0}<br />", Request.ApplicationPath));
      contextDiv.Innerhtml += (string.Format("<b>Application Relative Current Execution File Path: </b>{0} <br />", Request.AppRelativeCurrentExecutionFilePath));
      contextDiv.Innerhtml += (string.Format("<b>File Path: </b>{0} <br />",Request.FilePath));
      contextDiv.Innerhtml += (string.Format("<b>Page Path: </b>{0} <br />", Request.Path));
      contextDiv.Innerhtml += (string.Format("<b>Accept Types: </b>{0}<br />", Request.AcceptTypes));
      contextDiv.Innerhtml += (string.Format("<b>Used Browser: </b>{0}<br />", Request.Browser));
      contextDiv.Innerhtml += (string.Format("<b>Content Encoding: </b>{0}<br />", Request.ContentEncoding));
      contextDiv.Innerhtml += (string.Format("<b>Content Length: </b>{0}<br />", Request.ContentLength));
      contextDiv.Innerhtml += (string.Format("<b>Content Type: </b>{0}<br />", Request.ContentType));
      contextDiv.Innerhtml += (string.Format("<b>Used HTTP Method: </b>{0}<br />",    Request.HttpMethod));
      contextDiv.Innerhtml += (string.Format("<b>Header Body Content: </b>{0}<br />", Request.InputStream));
      contextDiv.Innerhtml += (string.Format("<b>Header: </b>{0}<br />", Request.Headers));
    }
  }
}
[/code]

Page_Load() Event Handler को उपरोक्तानुसार Program Logic Codes लिखते हुए Modify करने के बाद जब हम हमारे Web Project को Run करते हैं, तो हमें कुछ निम्नानुसार Output प्राप्त होता है:

Request Object or Request Context in ASP.NET WebForms in Hindi

हम हमारे Current Page पर Request Object को बिना Declare किए हुए इसलिए Directly Access कर सकते हैं, क्योंकि .NET Framework इस Object को हमारे लिए स्वयं ही Create कर देता है।

जबकि इस Object को हम इसलिए अपने सभी Webpages की Code-Behind File में Directly Access कर सकते हैं, क्योंकि Page Class हमारी सभी Code-Behind Classes की Base Class है। इसलिए Page Class में Define किया गया Request नाम का Property Object हमारी सभी Code-Behind Classes में Inherit हो जाता है, जिसे हम Directly Access करते हुए User Request से सम्बंधित विभिन्न Context Information को Retrieve कर सकते हैं।

साथ ही HttpRequest Class में Define की गई सभी Properties को public Access के साथ Define किया गया है, इसलिए हमारी हर Code-Behind Class में हम Request Object के माध्‍यम से User Request से सम्बंधित सभी Properties को Directly Access कर सकते हैं, जैसाकि उपरोक्त Code में किया है।

Request Object के लिए HttpRequest Class में बहुत सारी Properties को Define किया गया है, जिनके विषय में Detail से जानने के लिए हम Visual Studio के VIEW => Object Browser Tool को Use कर सकते हैं अथवा हम Microsoft की Online Resource Web Site को या फिर Microsoft के MSDN को भी Use कर सकते हैं।

इन Resources को Use करना इसलिए जरूरी है, क्योंकि .NET Framework में हजारों Classes है। और हर Class में सेकडों Properties, Methods Events हैं। लेकिन सभी Properties, Methods Events को याद रखना सम्भव नहीं है।

इसलिए बेहतर यही होता है कि हम Particular .NET Class द्वारा Provide की जाने वाली Functionality यानी Class के Concept को समझें कि किस जरूरत को पूरा करने के लिए किस Class को Use करना चाहिए, जबकि Class द्वारा Provide की जाने वाली

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