Virtual Path in ASP.NET

Kiwimoisi picture Kiwimoisi · Nov 29, 2011 · Viewed 17.5k times · Source

I am using this code for my LinkButton to download a file when you click on it. Recently i got this problem, i need a virtual path .

I would like to redirect to C:\inetpub\wwwroot

 string filepath = Server.MapPath("...");           
 FileInfo myfile = new FileInfo(filepath);

So I don't know what to put in Server.MapPath() because if I put / it will go to the base of my folder documents and no In

Answer

Dave Walker picture Dave Walker · Nov 29, 2011

If you need to serve files from another part of your system then the best way to do this is to create a virtual directory.

In IIS right hand click on your website, click 'Add Virtual directory'. Give it a name and point it whereever you need it to be. E.g. new virtual directory called 'files' pointing at 'C:\inetpub\wwwroot\files'.

Then from within your site you can reference these files by using

/files/filename.txt <- the /files/ will link to your virtual directory.

You will then of course need to make sure you have your permissions set correctly to read these files but I will leave that up to you.