0

Is there a function to determine whether a URI is a valid virtual path? I'm given a string and need to use Server.MapPath() on it without throwing an Exception when the string is not a valid virtual path.

Vote to close my question. Answer is @ asp.net - Is my path virtual?.

Community
  • 1
  • 1
JamesBrownIsDead
  • 73
  • 1
  • 1
  • 5

1 Answers1

0

You could use the File.Exists() and Directory.Exists() methods to check the output of Server.MapPath() and verify that a file/directory exists at the specified path.

Dim myPath as String = Server.MapPath('/some/path.aspx')
If File.Exists(myPath) Then
    //Do Something
Else
   If Directory.Exists(myPath) Then
       //Do Something
   Else
       //Invalid path
   End If
End If
jaywon
  • 8,164
  • 10
  • 39
  • 47
  • If "/some/path.aspx" is a full URL, Server.MapPath() will throw an HttpException. – JamesBrownIsDead Nov 17 '09 at 23:34
  • True, you can add exception handling for that case. In my experience, when using Server.MapPath, it has been on SERVER variables such as SCRIPT_NAME, which would not give you a full URL. In any case, glad you found your answer :) – jaywon Nov 17 '09 at 23:45