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?
michouisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robbert1979Commented:
Is your website and are your pdf files on the same server ?
so yes you can use something like this
if my.computer.filesystem.fileexists("path to your pdf file") then
...
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
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

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

michouisAuthor Commented:
I am still trying things out...
0
michouisAuthor Commented:
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!
0
CodeCruiserCommented:
So you were checking for the existence of page on your own server?
0
michouisAuthor Commented:
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!
0
CodeCruiserCommented:
But why would you need to check if the file exists or not on your own server(especially an aspx page)?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.