473,405 Members | 2,210 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,405 software developers and data experts.

Accessing Custom Attributes in an HttpModule

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 the attribute from global.asax using the Request and HttpContext as the target and it doesn't work (which I figured it wouldn't). If call System.Attribute.GetCustomAttribute() in the Page's load event I can access it by passing a reference to 'this' as the target param.

What should I pass to the GetCustomAttribute call in an HttpModule to have it inspect the uderlying page class that generated the current request being processed?

Thanks
Michael
Nov 18 '05 #1
3 3723
// get current page

System.Web.UI.Page myPage = HttpContext.Current.Handler;
-- bruce (sqlwork.com)
"Michael Iantosca" <ms*******@edefine.com> wrote in message
news:E8**********************************@microsof t.com...
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 the attribute from global.asax using the Request and HttpContext as the target and it doesn't work (which I figured it wouldn't).
If call System.Attribute.GetCustomAttribute() in the Page's load event I
can access it by passing a reference to 'this' as the target param.
What should I pass to the GetCustomAttribute call in an HttpModule to have it inspect the uderlying page class that generated the current request being
processed?
Thanks
Michael

Nov 18 '05 #2
Hi A.M,
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you've create a custom attribute for a Page class
which is used to perform as the base class of the pages in your web
application. Since you want to do some operations based on this custom
attribute at the begining of a request, you're looking for some approachs
to attrieve the Page's class in a certain HttpModule so as to use the class
to retreive the custom attribute.
If there is anything I misunderstood, please feel free to let me know.

Based on my research, in ASP.NET runtime, we can retrieve the request's
related page class(IHttpHandler) just as bruce barker said, using the
"HttpContext.Current.Handler". By default, if you haven't specified a
certain custom HttpHandler for the certain type of request,
"HttpContext.Current.Handler" is just the requested page's class. However,
the problem now is when could we retrieve it.
Generaly the whole sequence a ASP.NET request is processed is called the
ASP.NET pipeline. Once the request makes it in to the worker process, it
goes throught a series of steps and classes before it arrives at the
ultimate handler(by default is a certain page class). The simple sequence
is :

HttpWorkerRequest created--------->HttpRuntime class create a new instance
of HttpContext class ------------>the HttpContext instance generate other
necessary instance such as HttpRequest, HttpResponse... ----------->The
ApplicationFactory create a certain HttpApplication instance and the
HttpApplication instance initialize itself--------------> The HttpModules
are created instances and serve the request---------------->
HttpHandlerFactory create the instance of the correct HttpHandler(the page
class) and the HttpHandler calls its "ProcessReqeust" Method. Once the
ProcessRequest method returns, the request is complete.

For detailed infos on the ASP.NET runtime pipeline, you can view the
following tech article in MSDN:
#The ASP.NET HTTP Runtime
http://msdn.microsoft.com/library/en...PNETHTTPRuntim
e.asp?frame=true
So we could notice that the HttpHandler( the page class) is created at the
end of the pipeline. When the HttpModules instances are created, the
HttpHandler instance hasn't been created yet. So that means we can't
retrieve the Page class(HttpHandler) instance at that time.

Since the means using HttpModule fails, I've also tried retrieved the
HttpHandler in the Global object's Application events, such as
"Application_BeginRequest", "Application_EndRequest",
"Application_AuthenticateRequest" However, only in the
"Application_EndRequest" can I retrieve the page ( HttpHandler) instance
via the "HttpContext.Current.Handler", but I don't think the EndRequest is
the time you want.

So I haven't found a place other than the Page's event such as "Init" or
"Load" that could I got the HttpHandler instance so far. As you want to do
some modification based on the custom attribute of the page class, do you
think is possible to move the operation to other later event such as
"Page_Init" ? Or do you think we can look for any other means to workaround
this?

Please check out the preceding suggestions. If you have any questions or
need any help, please feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #3
Check out this article:
http://www.codeproject.com/aspnet/De...balization.asp. It shows how
to process custom attributes in a page and controls from a HttpModule.

Regards,
Sami

"Michael Iantosca" <ms*******@edefine.com> wrote in message
news:E8**********************************@microsof t.com...
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 the attribute from global.asax using the Request and HttpContext as the target and it doesn't work (which I figured it wouldn't).
If call System.Attribute.GetCustomAttribute() in the Page's load event I
can access it by passing a reference to 'this' as the target param.
What should I pass to the GetCustomAttribute call in an HttpModule to have it inspect the uderlying page class that generated the current request being
processed?
Thanks
Michael

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: ...
3
by: xAvailx | last post by:
I apologize ahead of time for the long post... Background: Working on a CRM type custom application. The application is for an event management company. The company will provide the application...
8
by: nicolas.sanguinetti | last post by:
Hi, I want to add custom attributes to my xhtml documents to use with my DOM scripts. For example, I want to have some tags -say, the <h1>- have an attribute and a . The thing is that I also...
1
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
1
by: Tamas Demjen | last post by:
I started to experiment with VC++ 2005 Beta1. So far everything went fine, and already have a working project, but soon I realized that the compiler was ancient (not supporting half of the C++/CLI...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
1
camel
by: camel | last post by:
Apologies if foolish question, but I somewhat expected Custom Attributes (i.e. inheriting from System.Attribute) decorating class properties to come through via WSDL to the proxy the same as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.