Microsoft KB Archive/810928

= HOW TO: Access ASP.NET Intrinsic Objects from .NET Components by Using Visual C# .NET =

Article ID: 810928

Article Last Modified on 5/21/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 319429.



For a Microsoft Visual Basic 6.0 version of this article, see 323259.



IN THIS TASK

 * SUMMARY
 * Create a .NET Component
 * Test the Component on an ASP.NET Page
 * REFERENCES



SUMMARY
This step-by-step article describes how to access the ASP.NET intrinsic objects in a Microsoft .NET component. As in Active Server Pages (ASP) pages, the ASP.NET pages have access to the intrinsic objects like Request, Response and Server objects.

back to the top

Create a .NET Component
To create a .NET component:  In Microsoft Visual Studio .NET, create a new Visual C# .NET Class Library project named ASPNetAccessLibrary. By default, Class1.cs is created. In Solution Explorer, right-click References, and then click Add Reference. In the Add Reference dialog box, select System.Web.dll, and then click Select. Click OK to add the System.Web.dll reference to the project.  Add the following code to the Namespace declaration section of Class1.cs: using System.Web; using System.Web.SessionState;   Add the following method to the Class1 class: public void TestHttpContext {           HttpContext objHttpContext;

HttpResponse objHttpResponse ; HttpRequest objHttpRequest ; HttpApplicationState objHttpApplication; HttpSessionState objhttpSession ; String strUserAgent ;

// Get the HttpContext object for the current HTTP request. objHttpContext = HttpContext.Current;

// Get the Application State object. objHttpApplication = objHttpContext.Application;

// Get the Session object. objhttpSession = objHttpContext.Session;

// Get the Response object. objHttpResponse = objHttpContext.Response;

// Get the Request object. objHttpRequest = objHttpContext.Request;

// This code uses the Request object. // You can use other intrinsic objects in a similar fashion strUserAgent = objHttpRequest.ServerVariables[&quot;HTTP_USER_AGENT&quot;];

// This code uses the Response object. objHttpResponse.Write(&quot;HTTP USER AGENT: &quot;); objHttpResponse.Write(strUserAgent); } </li> On the Build menu, click Build Solution to create the ASPNetAccessLibrary.dll component.</li></ol>

back to the top

Test the Component on an ASP.NET Page
To test the component on an ASP.NET page: <ol> In Visual Studio .NET, create a new Visual C# .NET ASP.NET Web Application project. By default, WebForm1.aspx is created.</li> In Solution Explorer, right-click References, and then click Add Reference.</li> In the Add Reference dialog box, click Browse. Locate and select ASPNetAccessLibrary.dll.</li> Click OK to add the .NET component reference to the project.</li> In Design view, double-click WebForm1.aspx to open the code-behind page for WebForm1.aspx. WebForm1.aspx.cs is displayed.</li>  Add the following code to the Page_Load event: ASPNetAccessLibrary.Class1 objASPNetLibrary; objASPNetLibrary = new ASPNetAccessLibrary.Class1; objASPNetLibrary.TestHttpContext; objASPNetLibrary = null; </li> On the Debug menu, click Start to view the results. Note that you can see HTTP_USER_AGENT details of the request on the Web page.</li></ol>

back to the top

<div class="references_section">