Search code examples
c#asp.net-mvciisfilesystemsvirtual-directory

IIS VirtualDirectories, and accessing their contents from a ASP.NET MVC Project


With Chromium no longer allowing links to local files I am attempting to test a couple of solutions to allow users to open PDF's on a local Network Share without having to download them first (the current work around).

I tested a pure JavaScript solution and that one worked great.

However I am trying use a Virtual Directory in IIS that points to a Network share with the files the user(s) can access.

When testing and trying to navigate to the file I have saved I get a "cannot find path error" enter image description here

I created a test application and published it on my local machine.

Below is the screenshot of the Virtual Directory I created. enter image description here


**Below** is the code I use to try and open the file.
[HttpPost]
        public ActionResult OpenPDF()
        {

            string directory = "./pdf";
            string file = "/light.pdf";
            byte[] fileBytes = System.IO.File.ReadAllBytes(directory + file);

            return File(fileBytes, "application/pdf");
        }

I assumed that the virtual directory would be in the root directory. I tried to find some examples of a Virtual Directory being accessed in code, and I haven't found one.

Resources I accessed:
https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/application/virtualdirectory

https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/create-virtual-directory-folder-remote-computer#:~:text=In%20the%20Internet%20Information%20Services,and%20then%20click%20Virtual%20Directory.

Virtual Directory to navigate to http://localhost/MyWebsite instead of http://localhost:8080

Different between ./ , ../ , ../../ , ~/ on file path(URL) in asp.net

https://www.c-sharpcorner.com/UploadFile/francissvk/create-virtual-directory-in-iis/

Any and all help would be greatly appreciated, thanks.


Solution

  • In ASP.NET Core you can return an VirtualFileResult

    A FileResult that on execution writes the file specified using a virtual path to the response using mechanisms provided by the host.

    Or you can do a Server.MapPath("~/pdf/light.pdf") to get the virtual path.

    Returns the physical file path that corresponds to the specified virtual path.

    In your example code you would use it as such:

    [HttpPost]
    public ActionResult OpenPDF()
    {
    
        string directory = "/pdf"; // notice I removed the . 
        string file = "/light.pdf";
        var filepath = Server.MapPath("~" + directory + file);
        byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
    
        return File(fileBytes, "application/pdf");
    }
    

    Keep in mind that if for access to a network share your Application Pool needs to run under an identity that has access to said network share.

    For a download of a file in a new tab try <a href="/file.pdf" target="_blank">file</a> in your client side html.