michouis
asked on
Check URL exists before redirect
I have an ASP web site written using VB.Net. I have a virtual directory with pdf files in it. I have a web page with links that perform a response.redirect to the pdf file so that the browser loads and displays the pdf file I am redirecting to.
Not all the pdf files may exist so what I need to a method of checking if the url I am redirecting to (pdf file) exists before attempting to response.redirect to it. I have tried using system.io.fileexists with and wthout using server.mappath and it always fails as if the file does not exist even when it does. So this method does not work.
How can I make sure the pdf file exists before issuing a response.redirect to it?
Not all the pdf files may exist so what I need to a method of checking if the url I am redirecting to (pdf file) exists before attempting to response.redirect to it. I have tried using system.io.fileexists with and wthout using server.mappath and it always fails as if the file does not exist even when it does. So this method does not work.
How can I make sure the pdf file exists before issuing a response.redirect to it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try adding this function to your page
Private Function URLExists(TargetURL as String) As Boolean
Dim req As WebRequest = WebRequest.Create(TargetURL)
Dim res As WebResponse
Try
res = req.GetResponse()
Return True
Catch
Return False
End Try
End Function
ASKER
I am still trying things out...
ASKER
Ah. I should have known since i already use this method to check if certain jpg images exist before attempting to display them... but either way, this is the correct solution. Thanks!
So you were checking for the existence of page on your own server?
ASKER
Yes. CodeCruiser, your code/suggestion was excellent too but, in the end, not the perfect one for this issue. Although your suggestion did teach me something new (how to use webresponse etc...). Thanks much!
But why would you need to check if the file exists or not on your own server(especially an aspx page)?
so yes you can use something like this
if my.computer.filesystem.fil
...
else
...
end if
You can also push the files to your clients so they can only access the files through your website instead of a direct link