How to create a virtual directory in IIS Express
Posted on Monday, 20th January 2014
I’m currently working on a rather large side project in ASP.NET MVC that requires an administrative area that’s best suited to its own project. Inside of this project is an upload folder that contains assets such as uploaded images a files. Whilst this is all fine and good I also have a separate project within the same solution that needs to reference some of these files that have been uploaded and have actions performed against them such as checking to see whether they exist and how large they are. Naturally these files are outside the directory of the project that needs access to them, so the best way to gain access to them is by creating a virtual directory.
Creating virtual directories is pretty straight forward when developing against IIS, however this particular project is using IIS Express I wasn’t too sure how easy this was to do, or whether it was even possible. However, it seems that it’s actually very straight forward to set up.
Creating a virtual directory in IIS Express
To create a Virtual Directory in IIS Express simple navigate to the IIS Express’ config directory located in _C:\Users<User>\Documents\IISExpress\config_ and open the applicationhost.config file.
The applicationhost.config file holds all configuration data for your Visual Studio projects that require IIS Express. As you browse through the file you’ll notice a sites section that contains your application(s) IIS Express configuration in the following format:
<site name="MvcApplication" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="c:\users\<User>\documents\visual studio 2012\Projects\MvcApplication\MvcApplication" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:1239:localhost" />
</bindings>
</site>
Once you’ve found this creating a virtual directory is very straight forward. Simply add an additional line like below:
<site name="MvcApplication" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="c:\users\<User>\documents\visual studio 2012\Projects\MvcApplication\MvcApplication" />
</application>
<application path="/Uploads" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\temp\YourVirtualDirectoryPath" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:1239:localhost" />
</bindings>
</site>
Once done load your project up and your virtual directory (in my case the /Uploads directory) will be accessible from within your project as if it were in the same directory.
Enjoy this post? Don't be a stranger!
Follow me on Twitter at @_josephwoodward and say Hi! I love to learn in the open, meet others in the community and talk Go, software engineering and distributed systems related topics.