asp.net-mvclocalhostvisual-studio-2017virtual-directory

How to setup a virtual directory for local host test run?


I have an ASP.net MVC 5 aplication with VS2017. I would like to load some pictures from a network path (the pictures should not be included to the project folder structure).

I know how to add a virtual directory in IIS on my server where the application is finally running. But I dont know how to setup the same on my local computer.

On the server I can setup an Alias and a physical path.

But in my project in VS2017(properties/web) I can only press the button "create virtual directory" next to project url "http://localhost:51138/".

Can maybe someone explain how to setup an virtual directory on localhost for testing? Thank you so much.


Solution

  • You are using the Visual Studio Development Server which does not allow virtual directories. You can do what you want with a local copy of IIS, creating a website that points to your development folder, and creating the virtual directory you want in that website.

    1. Install a local copy of IIS. This is available to you in the Professional version of Windows.

    2. Open IIS Manager, create a new website, and set the Physical Path to your development folder.

    3. Add a binding for this website with the Host name set to a domain name you want to use for testing. I generally use ".loc" as the TLD. For example, the production website domain of "mywebservice.com" would be "mywebservice.loc". Use port 80 in the binding.

    4. Modify your local hosts file at C:\Windows\System32\drivers\etc\hosts to include this line. Replace "mywebservice.loc" with whatever domain you chose to use above
      mywebservice.loc 127.0.0.1

    5. In your VS project properties, set Web-->Servers to Use Custom Web Server and set the Server Url to mywebservice.loc or whatever domain you chose above.

    Now you can add a virtual directory just as you do in production.

    I do this for all my website development primarily because nothing will happen in production that does not happen locally. You can even test using real SSL certificates this way.