Page Redirecting

  • Thread starter Thread starter damiensawyer
  • Start date Start date
D

damiensawyer

Hi,

I'm trying to do something in global.asax that I would have thought to
be quite simple. Basically, any request at all should get sent to
another page. I actually got the code below from a book. For some
reason, it's not working.

Can someone please tell me what I'm doing wrong?

Thanks in advance,


Damien

protected void Application_BeginRequest(object sender,
EventArgs e)
{
// this causes a "redirect loop"
Response.Redirect(Request.ApplicationPath + "/Forms/
LoginPage.aspx");

// This just "doesn't work" - standard page loads.
Context.RewritePath(Request.ApplicationPath + "/Forms/
LoginPage.aspx");
}
 
Hi,

I'm trying to do something in global.asax that I would have thought to
be quite simple. Basically, any request at all should get sent to
another page. I actually got the code below from a book. For some
reason, it's not working.

Can someone please tell me what I'm doing wrong?

Thanks in advance,


Damien

protected void Application_BeginRequest(object sender,
EventArgs e)
{
// this causes a "redirect loop"
Response.Redirect(Request.ApplicationPath + "/Forms/
LoginPage.aspx");

You have to check if it's the login page that is requested or not. Now
you are redirecting every request, even the ones that you are causing by
redirecting to the login page.
 
Your "redirect loop" is caused because you are not checking whether the
currently executing page is "LoginPage.aspx"

So even a successful redirect to LoginPage.aspx then redirects to
LoginPage.aspx, and the cycle continues.

I suggest checking the currently loaded page (Request.Url should help).

Otherwise, it might be worth looking at using a built in Authentication
provider like FormsAuthentication or similar to provide this redirect to
login functionality.

Good luck,
BWC
 
As it stands currently, your users will never be able to proceed any further than the login page...

Correct. Sorry - this wasn't the best example. I'm just 'playing
around' to test the functionality.

Out of interest, I got the response.redirect working by ommitting
calls to the login page, however, I can't get the context.rewrite to
happen. Googling around, I found a few posts of people having trouble
with it... none of them which seemed to have answers.

Thanks very much for everyone's help :-)
 
Back
Top