0

I need to check weather the requested resource by user in browser is present in my asp.net website directory or not and if it is not present than redirect user to error page.

As I have removed a number of pages from my website and they already got indexed in google bot or sometimes typed by user directly in url.

so whenever request like this made from the browser, my code should do something like following;

if(requested url does not exist)
{
   Response.Redirect("error.aspx");
}

how to achieve that programatically. Thanks.

Learner
  • 776
  • 6
  • 14
  • 40
  • possible duplicate of [C# How can I check if a URL exists/is valid?](http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid) – bumbumpaw Jun 30 '14 at 04:00
  • @Niang Not a duplicate. The link you provided discusses how to check the existence of a page remotely. This question discusses from within the ASP .NET environment. – rhughes Jun 30 '14 at 04:21

1 Answers1

0

If i am not wrong this would be the solution you are looking for:

string URL ="mypage.aspx";
if (File.Exists(Server.MapPath(URL)))     

{
   Response.Redirect(URL,false);
}
else
{ 
   Response.Redirect("~\\404PageNotFound.aspx", true); 
}
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143