By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 7 Jul 2005 20:26:12 -0400, "Steve Franks"
<pl****@postreplyhere.com> wrote:
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


Nov 19 '05 #2

P: n/a
> 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
Nov 19 '05 #3

P: n/a
>
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.