Accessing Custom Attributes in an HttpModule

G

Guest

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
 
B

bruce barker

// get current page

System.Web.UI.Page myPage = HttpContext.Current.Handler;


-- bruce (sqlwork.com)


Michael Iantosca said:
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?
 
S

Steven Cheng[MSFT]

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-us/dnaspp/html/dngrfTheASPNETHTTPRuntim
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.)
 
S

Sami Vaaraniemi

Check out this article:
http://www.codeproject.com/aspnet/DeclarativeGlobalization.asp. It shows how
to process custom attributes in a page and controls from a HttpModule.

Regards,
Sami

Michael Iantosca said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top