I have an installation of IIS Express 10 on my local machine that I use for developing MVC applications. Normally, I use the following configuration in applicationhost.config for my sites, where I have no virtual directories defined:
<site name="Development Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="C:\dev\MyMVC" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" />
</bindings>
</site>
Using the above configuration, the MVC application can be accessed with no errors at http://localhost:8080
However, I recently tried to make the application accessible using a virtual directory (please note the path "C:\fake" as the root directory--IIS requires a site's root directory to be defined before any virtual directories are--see How to configure a different virtual directory for web site project with Visual Studio 2015). I created the virtual directory using the following configuration in applicationhost.config:
<site name="Development Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="C:\fake" />
</application>
<application path="/test/app">
<virtualDirectory path="/" physicalPath="C:\dev\MyMVC" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" />
</bindings>
</site>
Using the above configuration and navigating to http://localhost:8080/test/app, the MVC application begins to load, but I get the following error:
c:\dev\MyMVC\views\index.cshtml(14): error CS0103: The name 'ViewBag' does not exist in the current context
What is the virtual directory configuration doing to cause this error? It works fine without the virtual directory.
You can replicate this error using a fresh MVC template in Visual Studio, without touching any code other than what's in the applicationhost.config file, so I don't think the problem is in my code.
It turned out, that not only does IIS Express require a root directory for the site as a whole to be defined, but also every virtual directory URL segment as well. So, it worked after changing the configuration to this (note the intermediate "/test" virtual directory):
<site name="Development Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="C:\fake" />
</application>
<application path="/test">
<virtualDirectory path="/" physicalPath="C:\fake" />
</application>
<application path="/test/app">
<virtualDirectory path="/" physicalPath="C:\dev\MyMVC" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" />
</bindings>
</site>
http://localhost:8080/test/app now works properly without the 'ViewBag' error