MattSlay
MattSlay

Reputation: 9995

In Asp.Net, check for existence of aspx page before redirecting to it?

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.

Upvotes: 3

Views: 3908

Answers (4)

m3kh
m3kh

Reputation: 7941

System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists("~/SomePage.aspx");

Upvotes: 0

user1228
user1228

Reputation:

File.Exists(Server.MapPath("~/SomePage.aspx"))

Upvotes: 3

Brandon
Brandon

Reputation: 70012

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.

Upvotes: 8

Shoban
Shoban

Reputation: 23016

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

Upvotes: 0

Related Questions