473,762 Members | 6,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cur rent.Request.Bu iltInClass
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 HttpBrowserCapa bilities class. So
from any ASP.NET page you can just do HttpContext.Cur rent.Request.Br owser 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 2974
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.Cur rent.Applicatio nInstance as ASP.Global_asax ;

The ApplicationInst ance 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****@postrep lyhere.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.Cur rent.Request.Bu iltInClass
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
automaticall y provides an instance of the HttpBrowserCapa bilities class. So
from any ASP.NET page you can just do HttpContext.Cur rent.Request.Br owser 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
2300
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 page.I am using the Custom thread pool as given in the article's sample. Implementation: =============== In AsyncPage.aspx I inhereted AsyncPage class instead of System.Web.UI.Page. SyncPage.aspx is like any other Web page which inherits
3
3760
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 class. If is attached I want to perform some action. How can I access custom attributes from an HttpModule? I have to pass a target to the System.Attribute.GetCustomAttribute() call to attempt to retrieve the attached attribute. I tried to access...
3
7257
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, when I check the Page.User property, it's a WindowsPrincipal and not _my_ custom Iprincipal. Why would this be? .... using System;
1
2643
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 good idea ? I'm coming from ASP and Session variables, but I explicitly wanted to avoid that for .NET. The problem I'm facing is that I don't find a good way to bring my source
3
1546
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 want to construct a Page class instance and load my other controls to it and return the page instance. This is what .net framework does. There is a small difference what i am thinking and the .net framework has done. .Net Framework calls the...
2
2149
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. : HttpContext.Current.Response.Cookies.Add("myNewCookie"); I am wondering if there are any landmines that I should know about, or if this will work pretty much as I am expecting a cookie should.
8
3900
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 need to avoid the extra round-trip to the client. I've tried Passing the page name, the full URL, and the instance of the handler class to the Transfer method, but everything gets me the same error 500. Any help would be appreciated.
1
6336
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 soap header called by using a http module. The problem Im having is I cannot seem to get the event to raise to fire off my authenticate method in the global.asax. The module is plumbed in to my web.config file Code Below:-
3
2028
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 project and I get the same errors there: Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers.
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.