Can I add a custom class to the HttpContext (ASP.NET 2.0)?

S

Steve Franks

Is there a way I can extend the HttpContext or one of its subclasses to
include a property that exposes a custom class of mine to all ASP.NET pages?

More specifically, I'd like to use a HttpModule to initialize an instance of
a custom class, and have this class exposed directly through the HttpRequest
for the current user through a property I add to the HttpRequest object.

For example, I'd like my developers to be able to use this special class
just like it was a built-in part of the ASP.NET framework, like so:
MyBuiltInClass myclass = HttpContext.Current.Request.BuiltInClass
where, BuiltInClass is a property that holds an instance of MyBuiltInClass

Is there any way to do this?

To clarify, there is a Browser property on the HttpRequest class that
automatically provides an instance of the HttpBrowserCapabilities class. So
from any ASP.NET page you can just do HttpContext.Current.Request.Browser to
get a copy of this class. Well, I would like to create the same type of
system where "behind the scenes" I create a MyBuiltInClass instance for each
HttpContext and then exposre it to all pages through the Request object?

Can this be done using ASP.NET 2.0? Obviously this is done currently as
part of the built-in framework (as in the case of the Browser property I
mentioned above), but I'm not sure if the framework is extensible enough for
me to accomplish this for a custom class. If not, is there some other way
that is just as effective for me to accomplish the same thing? Any ideas?

Thank you in advance.

Steve
 
S

Scott Allen

Hi Steve:

Is the object you want to carry around with the Request going to be
specific to each request?

There are a couple options I can think of. One would be to keep an
instance of the class in the HttpContext Items collection - this would
need to be initialized with each request.

You can also add custom properties to the class generated from
global.asax. After adding global.asax to the project you could do:

<%@ Application Language="C#" %>

<script runat="server">

public override void Init()
{
// init custom property, then init base
base.Init();
}

public MyClass CustomProperty
{
get { return ... }
set { ... }
}

</script>

You can retrieve an instance of the application class using:

HttpContext.Current.ApplicationInstance as ASP.Global_asax;

The ApplicationInstance object are pooled - You can retrieve an
instance of the application class using:
 
S

Steve Franks

Is the object you want to carry around with the Request going to be
specific to each request?

Yes. Well, not necessarily each request but more like each user session.
So to answer your question, its more like the object I want to carry around
is specific to each user session. I could put it in the session object, but
I'd prefer instead if I could hook it right into the Request object in a
strongly typed manner, so users of my assembly could just doin
Request.MyClass and access it.
There are a couple options I can think of. One would be to keep an
instance of the class in the HttpContext Items collection - this would
need to be initialized with each request.

Thanks. I didn't realize that existed. Ideally what I'd like to do is to
add a strongly typed member to the Request class. Guess I can't do that,
right?

Steve
 
S

Scott Allen

Thanks. I didn't realize that existed. Ideally what I'd like to do is to
add a strongly typed member to the Request class. Guess I can't do that,
right?

I don't believe there is anything exposed or available to do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top