Page Redirecting

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");
}
 
G

Göran Andersson

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

BWC

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
 
D

damiensawyer

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 :)
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top