views:

280

answers:

4

How can I check for the existence of an aspx page before attempting to redirect to it, so I can handle that case in my C# code?

Response.Redict("~/SomePage.aspx")

But I want to make sure that page really does exist before I call it. It works off of a string after all, so maybe I have a type or something, or maybe I have not created that page yet.

A: 

If the file is on the same server then You can check if the file exists and then redirect the user.

Shoban
+7  A: 

Do a File.Exists on the page.

  if(File.Exists(Server.MapPath("~/SomePage.aspx")))
    Response.Redirect("~/SomePage.aspx");

You'll need to use the System.IO namespace.

Brandon
Thanks for the extra hint about the namespace.
MattSlay
I got this additional info from another source. Just FYI to other users:MapPath() only works within the virtual directory structure. ie. you can't navigate above the virtual directory with it and that's actually by design for security issues.
MattSlay
+3  A: 
File.Exists(Server.MapPath("~/SomePage.aspx"))
Will
A: 
System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists("~/SomePage.aspx");
Mehdi Golchin