Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Application_BeginRequest and the Page object

Reply
Thread Tools

Application_BeginRequest and the Page object

 
 
Mark Rae
Guest
Posts: n/a
 
      08-18-2006
"Kevin Jones" <(E-Mail Removed)> wrote in message
news:O4psU%(E-Mail Removed)...

Hi Kevin,

> Page p = Context.Handler as Page;
>
> if(p != null)
> {
> // use Page here
> }


Thanks for that - problem is that p is always null...

Mark


 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      08-18-2006
re:
> Can you please tell me what it is? I'd be really grateful.


Kevin beat me to it....

Try

Page p = Context.Handler as Page;

if(p != null)
{
// use Page here
}

Sorry if you are miffed that I was attempting to dissuade you from
using that type of code but I, honestly, don't see much use in using it.

Maybe you could explain to me why it is important to you ?

i.e., can you tell me why detecting Page properties in
Application_BeginRequest won't result in superfluous code




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
===================================
"Mark Rae" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Again, the basic problem is that that code code execute for *all* requests.

>
> I understand that - and that will be my problem, not yours...
>
>> I'm not sure that you want Page properties code executed all the time but if you do...

>
> Juan, you're not an unintelligent man, so you are obviously just "playing dumb" with me here...
> That's fine - I've no problem with a little ribbing - do it myself more often than not...
>
> But, you're an MVP, and you know *perfectly well* what I'm asking, so would it be possible for you
> to actually tell me the answer now...?
>
> However, just to reiterate, I'm looking for a way, a process, a whatever to reference the Page
> object from within the Application_BeginRequest method of Global.aspx.cs
>
> I know that this will execute for *all* requests - I almost certainly won't implement it - I ask
> merely for my own interest and to further my understand of ASP.NET.
>
> You've already told me that it is possible, i.e. you know how to do it and I don't.
>
> So, once again, can you please tell me how to reference the Page object from within
> Application_BeginRequest? Just so there is no ambiguity, I'm specifically looking for the
> namespace / object(s) to take the place of the <page> token in the pseudo-code below:
>
> protected void Application_BeginRequest(Object sender, EventArgs e)
> {
> <page>.<property> = <someValue>;
> }
>
> Can you please tell me what it is? I'd be really grateful.



 
Reply With Quote
 
 
 
 
Kevin Jones
Guest
Posts: n/a
 
      08-18-2006
The value of p will depend on the type of content you are requesting.
Context.Handler contains a reference to the IHttpHandler that is
handling the request, if you send a request to a .aspx page then the
IHttpHandler will be the page you created so p shouldn't be null.

Try putting a break point on the Context.Handler as Page line and see
what type it is,

Kevin
 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      08-18-2006
You might want to consider using Page_PreInit or Page_Init to execute code
which affects a particular page, instead of using Application_BeginRequest, which affects all pages.

There's other Page events which you might want
to consider as places where to execute Page code events.

For a complete list, see : http://msdn2.microsoft.com/en-us/library/ms178472.aspx



Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
===================================
"Mark Rae" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> "Kevin Jones" <(E-Mail Removed)> wrote in message
> news:O4psU%(E-Mail Removed)...
>
> Hi Kevin,
>
>> Page p = Context.Handler as Page;
>>
>> if(p != null)
>> {
>> // use Page here
>> }

>
> Thanks for that - problem is that p is always null...
>
> Mark
>





 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      08-18-2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Maybe you could explain to me why it is important to you ?


Interest. Learning.

> i.e., can you tell me why detecting Page properties in
> Application_BeginRequest won't result in superfluous code


I'm certain that it would...


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      08-18-2006
"Kevin Jones" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> The value of p will depend on the type of content you are requesting.
> Context.Handler contains a reference to the IHttpHandler that is handling
> the request, if you send a request to a .aspx page then the IHttpHandler
> will be the page you created so p shouldn't be null.


It always is...

> Try putting a break point on the Context.Handler as Page line and see what
> type it is,


It's always null... In the Immediate Window, if I ask it to display
Context.Handler.GetType(), the result is always:

'((object)(((System.Web.HttpApplication)(this)).Co ntext.Handler))' is null



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      08-18-2006
Mark,
sorry about getting thrown off by the reference to Master pages. The bottom
line:
Application_BeginRequest is fired at the very, VERY beginning of the entire
request processing pipeline, way before any instance of the Page class is
created.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Mark Rae" wrote:

> "Kevin Jones" <(E-Mail Removed)> wrote in message
> news:O4psU%(E-Mail Removed)...
>
> Hi Kevin,
>
> > Page p = Context.Handler as Page;
> >
> > if(p != null)
> > {
> > // use Page here
> > }

>
> Thanks for that - problem is that p is always null...
>
> Mark
>
>
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      08-18-2006
"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

Peter,

> sorry about getting thrown off by the reference to Master pages.


S'OK - I now see it was my fault for not phrasing my question more
clearly...

> The bottom line:
> Application_BeginRequest is fired at the very, VERY beginning of the
> entire
> request processing pipeline, way before any instance of the Page class is
> created.


Hence, the very first line of my original post...

"Is it possible to have programmatic access to the Page object in
Application_BeginRequest, or is it too early in the lifecycle...?"

So, is it or isn't it? Is this why the code suggested by Kevin always
returns the Page object as null?

Mark


 
Reply With Quote
 
Kevin Jones
Guest
Posts: n/a
 
      08-18-2006
>> The bottom line:
>> Application_BeginRequest is fired at the very, VERY beginning of the
>> entire
>> request processing pipeline, way before any instance of the Page

class is
>> created.



> So, is it or isn't it? Is this why the code suggested by Kevin always
> returns the Page object as null?


Yep, sorry. The Handler doesn't exist and so is not set in the context
until the PreRequestHandlerExecute event (which is where I've always
done this in my code)

Kevin

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      08-18-2006
"Kevin Jones" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> Yep, sorry. The Handler doesn't exist and so is not set in the context
> until the PreRequestHandlerExecute event (which is where I've always done
> this in my code)


Well that pretty much answers my original question - thanks.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application_BeginRequest and Request.Params[] under Asp.net 2.0 tao lin ASP .Net 1 03-26-2006 10:47 PM
Java script error when URL's modified in Application_EndRequest and Application_BeginRequest Wayne Sepega ASP .Net 1 01-06-2006 03:24 PM
Catch WebMethod name and params in Application_BeginRequest Franck ASP .Net 1 10-04-2005 11:26 AM
Catch WebMethod name and params in Application_BeginRequest Franck ASP .Net Web Services 0 09-30-2005 06:47 AM
Can Application_BeginRequest return a DataSet and stop normal webmethod flow? ASP .Net Web Services 1 05-25-2005 06:02 PM



Advertisments