Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Access ASP.NET Intrinsic Objects from .NET Components Using Visual Basic .NET


View products that this article applies to.

This article was previously published under Q319429
For a Microsoft Visual C# .NET version of this article, see 810928 (http://support.microsoft.com/kb/810928/EN-US/ ) .
For a Microsoft Visual Basic 6.0 version of this article, see 323259 (http://support.microsoft.com/kb/323259/EN-US/ ) .
For a Microsoft Visual C# .NET version of this article, see 810928 (http://support.microsoft.com/kb/810928/EN-US/ ) .
For a Microsoft Visual Basic 6.0 version of this article, see 323259 (http://support.microsoft.com/kb/323259/EN-US/ ) .

↑ Back to the top


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.

Create a .NET Component

To create a .NET component:
  1. In Microsoft Visual Studio .NET, create a new Visual Basic .NET Class Library project named ASPNetAccessLibrary. By default, Class1.vb is created.
  2. In Solution Explorer, right-click References, and then click Add Reference.
  3. In the Add Reference dialog box, select System.Web.dll, and then click Select.
  4. Click OK to add the System.Web.dll reference to the project.
  5. Add the following code to the Namespace declaration section of Class1.vb:
    Imports System.Web
    Imports System.Web.SessionState
    
  6. Add the following method to the Class1 class:
    	Public Sub TestHttpContext()
    
          Dim objHttpContext As HttpContext
    
          Dim objHttpResponse As HttpResponse
          Dim objHttpRequest As HttpRequest
          Dim objHttpApplication As HttpApplicationState
          Dim objhttpSession As HttpSessionState
          Dim strUserAgent As String
    
          ' 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("HTTP_USER_AGENT")
    
          ' This code uses the Response object.
          objHttpResponse.Write("HTTP USER AGENT: ")
          objHttpResponse.Write(strUserAgent)
    
       End Sub
  7. On the Build menu, click Build Solution to create the ASPNetAccessLibrary.dll component.

Test the Component on an ASP.NET Page

To test the component on an ASP.NET page:
  1. In Visual Studio .NET, create a new Visual Basic .NET ASP.NET Web Application project. By default, WebForm1.aspx is created.
  2. In Solution Explorer, right-click References, and then click Add Reference.
  3. In the Add Reference dialog box, click Browse. Locate and select ASPNetAccessLibrary.dll.
  4. Click OK to add the .NET component reference to the project.
  5. In Design view, double-click WebForm1.aspx to open the code-behind page for WebForm1.aspx. WebForm1.aspx.vb is displayed.
  6. Add the following code to the Page_Load event:
    	Dim objASPNetLibrary As ASPNetAccessLibrary.Class1
          objASPNetLibrary = New ASPNetAccessLibrary.Class1()
          objASPNetLibrary.TestHttpContext()
          objASPNetLibrary = Nothing
  7. 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.

↑ Back to the top


References

For more information about HttpContext, visit the following Microsoft Developer Network (MSDN) Web site:

↑ Back to the top


Keywords: KB319429, kbhowtomaster, kbwebforms

↑ Back to the top

Article Info
Article ID : 319429
Revision : 8
Created on : 5/19/2003
Published on : 5/19/2003
Exists online : False
Views : 503