Recipe 21.2. Accessing HTTP-Specific Information from Within a Class


Problem

You want to create a business service class that can be used by any page in your site, and you want it to have access to the HTTP-specific information available in web pagesthat is, all the server objects used by the application.

Solution

Add a reference to the System.Web assembly in your business service project and a companion Imports statement (or using statement in C#) to your class, and use the Current property of the HttpContext object to access the desired server objects.

In the business service class, use the .NET language of your choice to:

  1. Add a reference to System.Web.

  2. Import the System.Web namespace.

  3. Reference the current HTTP context when accessing server objects, as in HTTPContext.Current.Session.

Examples 21-1 and 21-2 show the VB and C# class files for an example business service that implements this solution.

Discussion

By referencing the Current property of the HttpContext object in the business class, your code has full access to all the server objects used in web applications. This includes the ability to access all information about the request being made, the response being returned, session data, and application data. For more information, refer to the HttpContext class in the MSDN documentation.

When you create an ASP.NET application with Visual Studio .NET, all the pages and classes of the web project have access to the HTTP-specific information. This is because Visual Studio automatically adds a reference to System.Web when you create the project. When you create a Class Library project, the technique commonly used to create reusable assemblies, Visual Studio does not automatically add the reference to System.Web. You can do so manually, however, by right-clicking the project in the Solution Explorer and selecting Add Reference from the context menu. When the Add Reference dialog box appears, choose the System.Web.dll component from the .NET tab.

If you are not using Visual Studio for your project, you can add the reference as part of the compile command. The following fragments show how we do this for our example:

 

vbc /target:library /reference:System.Web.dll /out:BusinessService.dll BusinessService.vb

csc /target:library /reference:System.Web.dll /out:BusinessService.dll BusinessService.cs

In the class requiring access to the HTTP-specific information, add an Imports statement (or using statement in C#) for the System.Web assembly. Adding this statement imports the namespace and provides access to the HTTP objects without having to fully qualify each reference to the object, making the code more readable and easier to maintain. The difference in access methods is shown here:

 

'access to the Session object when Imports statement is included value = CStr(HttpContext.Current.Session("someData")) 'access to the Session object when the Imports statement is NOT included value = CStr(System.Web.HttpContext.Current.Session("someData"))

// access to the Session object when Imports statement is included value = (string)(HttpContext.Current.Session["someData"]); // access to the Session object when the Imports statement is NOT included value = (string)(System.Web.HttpContext.Current.Session["someData"]);

By adding the reference and Imports (or using) statement to your business service projects, your code has full access to all the server objects used in web applications.

Providing access to the server objects in your business classes is useful and necessary for some services, but it should not be universally applied. When your business service requires access to the HTTP-specific information, it can no longer be used in non-web applications, reducing its reusability.


See Also

HttpContext class documentation in the MSDN Library

Example 21-1. Accessing HTTP-specific information (.vb)

 Imports System.Web Public Class BusinessService   Public Sub doSomething() Dim value As String value = CStr(HttpContext.Current.Session("someData")) 'use the data from session as required   End Sub End Class 'BusinessService 

Example 21-2. Accessing HTTP-specific information (.cs)

 using System.Web; namespace CSMisc {   public class BusinessService   { public BusinessService() {   string value;   value = (string)(HttpContext.Current.Session["someData"]);   // use the data from session as required }   } } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net