473,385 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 2946
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
> 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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
3
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom attribute is attached to the underlying page...
3
by: David B. Bitton | last post by:
For some odd reason, despite the fact that I assign my own custom IPrincipal to the HttpContext.User property in an HttpApplication.AuthenticateRequest event handler inside of an IHttpModule,...
1
by: Beren | last post by:
Hello With trial and error I'm attempting to create an extended identity to store some more data than just the Name, for example a Subscription and a LastSearchPerformed property... Is this a...
3
by: Hope Paka | last post by:
I want to use my custom url extension instead of .aspx. I can achieve this by writing a custom handler that implements the IHttpHandlerFactory. In the GetHandler method of the IHttpHandlerFactory i...
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
8
by: bryan | last post by:
I've got a custom HttpHandler to process all requests for a given extension. It gets invoked OK, but if I try to do a Server.Transfer I get an HttpException. A Response.Redirect works, but I really...
1
by: Stu | last post by:
Hi, Im using vis studio 2003 and I think wse is out of the question as clients could be using java which doesnt support it. So I managed to find some code which allows you to develop a custom...
3
by: Mark Leistner | last post by:
I am having problems getting a gridview to bind to custom objects under any non-full trust level. I created a test project to verify what I am seeing isn't a side effect of other code in my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.