Published: 08 Nov 2006
By: Brian Mains

Whether you know it or not, it is possible to access certain ASP.NET classes in Windows components, such as HttpContext that provides you with access to many of the needed objects in the ASP.NET runtime. This article is going to explain how it can work.

About HttpContext

HttpContext provides a means to access certain web-based components when at the page, user control, business object, etc. level. HttpContext is created by the .NET framework so that the IHttpHandler and IHttpModule objects that the framework run on have access to key ASP.NET objects they may need. This class is exposed through the HttpContext.Current property (a static implementation representing the current request). You may notice that the Page object implements some of the same features; in fact, the properties that the Page object duplicates are being returned from the HttpContext object.

With that said, here is how we can use it:

The above code will write a message directly to the response stream, and so we will see the message "This is my message" in the browser. This is one of the great benefits of this class.

Using HttpContext In Components

You can use HttpContext in your components to access security related information. For instance, to get the user name of a person (if authenticated), you can do this:

You can also access the Request and Response streams through:

HttpContext provides you with resources to the HttpServerUtility class (exposed as the Server property), tracing capabilities, application/session information, caching, and much more. It is because HttpContext is instantiated during the lifecycle of the application that it is available in your components store in App_Code or in a class library. The Current property represents the current request, and you have to ensure that the object isn't null before using it. For example, when trying to get the user information of a windows authenticated user, you should do:

About Membership and Roles

In addition to HttpContext, we can also use the Membership and Roles objects to get information about the Membership and Roles of an application. These objects have more requirements; remember that they use a configuration section in the element, containing a and elements as children. Regardless, you can set this up in a web or windows application, which makes it versatile to use throughout your application. Membership and Roles are static classes, so you can do things like:

I was surprised at this first, whenever I saw this in the Microsoft hands-on examples for Enterprise Library security block; but after thinking about it, it is essentially a class and does make very much sense. So you can use it in the web and windows environment; whenever working in the windows environment, you have to use the GetUser() override where you can specify the user name, like I did in my previous example.

Summary

As you can see, the HttpContext and other web objects are accessible to you components exposed in an ASP.NET application, or in the windows environment, providing you with a lot more capabilities in your applications.

References

Check out the HttpContext Members to find out more about what is available. Also, check out this article on DotNet John to see how to use windows accounts for Membership, but application roles with the Roles API.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Brian Mains

Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare. In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...

This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
jQuery Mobile Pages
Brian Mains explains how to create pages with the jQuery Mobile framework.
Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...

You might also be interested in the following related blog posts


Welcome the WebUI Test Studio v2.0! read more
Introducing Recurring Appointments for Web.UI Scheduler ASP.NET AJAX read more
November's Toolbox Column Now Online read more
Web-based Scheduling Solution with the WOW-Factor for ASP.NET 2.0 and Up read more
Q3 2009 Release Week kicks-off November 4th, Free Daily Webinars read more
Sneak Peak: CSS Sprites Make Your Websites Faster read more
ASP.NET 4 Web Server Here Shell Extension read more
New articles published on Ajax performance and futures read more
Upgrade Wizard: auto-upgrades and more read more
Adhoc testing of .NET RIA Services read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.