Application_BeginRequest and the Page object

M

Mark Rae

Hi,

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

E.g. to be able to change a page's MasterPage dynamically, something like:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Session["LoggedOn"] == "True")
{
<page>.MasterPageFile = "~/loggedOn.master";
}
else
{
<page>.MasterPageFile = "~/notLoggedOn.master";
}
}

N.B. I realise it's possible to achieve the above functionality in other
ways - it's just a hypothetical example...

Any assistance gratefully received.

Mark
 
J

Juan T. Llibre

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

The problem is that the Application_BeginRequest event is raised for *all* requests.

Are you sure you want that code to execute every time your application receives a request?

Check out the Application Lifecycle Overview at :
http://msdn2.microsoft.com/en-us/library/ms178473.aspx

and the ASP.NET Page Life Cycle Overview, at :
http://msdn2.microsoft.com/en-us/library/ms178472.aspx

I'm sure you'll find a more appropiate application, or page,
event for that code in one of those two pages.





Mark Rae said:
Hi,

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

E.g. to be able to change a page's MasterPage dynamically, something like:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Session["LoggedOn"] == "True")
{
<page>.MasterPageFile = "~/loggedOn.master";
}
else
{
<page>.MasterPageFile = "~/notLoggedOn.master";
}
}

N.B. I realise it's possible to achieve the above functionality in other ways - it's just a
hypothetical example...

Any assistance gratefully received.

Mark
 
M

Mark Rae

The problem is that the Application_BeginRequest event is raised for *all*
requests.

That's right.
Are you sure you want that code to execute every time your application
receives a request?

Who knows - was just interested to know, hence the "hypothetical" bit... :)

Is it possible?
 
M

Mark Rae

Peter,
Scott Allen has a very in-depth piece all about neat tricks with
MasterPages:

I was merely using MasterPages as a hypothetical example, hence the sentence
"N.B. I realise it's possible to achieve the above functionality in other
ways - it's just a hypothetical example..."

Do you know if it's possible to have programmatical access to the Page
object in Application_BeginRequest NOT NECESSARILY for anything related to
MasterPages...?
 
M

Mark Rae

Yes it is possible
Cool.

That code will execute every time your application receives a request.

Understood.

Can you please tell me how to reference the Page object in
Application_BeginRequest. E.g.

protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Session["LoggedOn"] == "True")
{
<page>.<property> = <someValue>;
}
else
{
<page>.<property> = <someOtherValue>;
}
}
 
J

Juan T. Llibre

re:
Can you please tell me how to reference the Page object in Application_BeginRequest. E.g.

Do you mean as in :

If Page.IsPostBack

?

Try it...

Again, the basic problem is that that code code execute for *all* requests.
I'm not sure that you want Page properties code executed all the time but if you do...





Mark Rae said:
Yes it is possible
Cool.

That code will execute every time your application receives a request.

Understood.

Can you please tell me how to reference the Page object in Application_BeginRequest. E.g.

protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Session["LoggedOn"] == "True")
{
<page>.<property> = <someValue>;
}
else
{
<page>.<property> = <someOtherValue>;
}
}
 
K

Kevin Jones

Try

Page p = Context.Handler as Page;

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

Kevin Jones
 
M

Mark Rae

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.
 
J

Juan T. Llibre

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
 
K

Kevin Jones

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
 
J

Juan T. Llibre

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
 
M

Mark Rae

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)).Context.Handler))' is null
 
M

Mark Rae

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
 
K

Kevin Jones

The bottom line:
> 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
 
M

Mark Rae

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.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top