468,292 Members | 1,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,292 developers. It's quick & easy.

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 3434
// 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Doug Holland | last post: by
3 posts views Thread by xAvailx | last post: by
8 posts views Thread by nicolas.sanguinetti | last post: by
1 post views Thread by Bret Pehrson | last post: by
3 posts views Thread by Edward Diener | last post: by
1 post views Thread by Tamas Demjen | last post: by
2 posts views Thread by prabhupr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.