Testing if URL exists and then redirecting to another page if fail

G

Guest

I have had no luck with the following scenario:

One of our requirements for a particular business scenario is to test the
existance of a URL before sending the client there. If the URL does not
exist, then we send them to another page.

To test the destination page, I use this code:

bool foundUrl = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
try
{
request.Method = "HEAD";
request.GetResponse();

//no probs so continue to destination
HttpContext.Current.Response.Redirect(destinationUrl, true);
}
catch (WebException webEx)
{
foundUrl = false;
}
finally
{
if (requestCheck != null)
{
requestCheck.Abort();
}
}

The problem I have now is that Response is stuck in Status 404. It will not
allow me to Redirect. I have tried the following workarounds to no avail:

1) Response.Write("<script
language=javascript>window.navigate('mynewpage.aspx');</script>")
Response.End

2)
Response.ClearHeaders();
Response.ClearContent();
Response.Redirect('mynewpage.aspx');
 
G

Guest

Noremac said:
I have had no luck with the following scenario:

One of our requirements for a particular business scenario is to test the
existance of a URL before sending the client there. If the URL does not
exist, then we send them to another page.

To test the destination page, I use this code:

bool foundUrl = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
try
{
request.Method = "HEAD";
request.GetResponse();

//no probs so continue to destination
HttpContext.Current.Response.Redirect(destinationUrl, true);
}
catch (WebException webEx)
{
foundUrl = false;
}
finally
{
if (requestCheck != null)
{
requestCheck.Abort();
}
}

The problem I have now is that Response is stuck in Status 404.

How have you come to that conclusion? What happens when you try? Do you
get any error message?

There is no reason that the HttpWebRequest object would affect the
current response in any way.
 
W

Walter Wang [MSFT]

Hi Noremac,

I'm not sure if I fully understand your question: do you mean that after
requesting an existing page, the statement
"HttpContext.Current.Response.Redirect..." will fail? I cannot reproduce
the issue on my side using a simple webform. Maybe it's related to your
other code or specific configuration?


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Yep, after I had some time today to finally throw the code in a test app it
worked for me too. Went back to my real code and it turned out a wrapper
object to HttpContext.Current was wrong. Now works for me too.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top