0

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.

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • What have you tried? Other posts indicate cache folder cleanup and so on, but you seem to try nothing at all yet. – Lex Li Apr 14 '18 at 03:33
  • @LexLi there is nothing to try in code, as the error is displayed without even touching the code (you can reproduce the error with a new, fresh, canned Visual Studio MVC template) – user3163495 Apr 14 '18 at 04:14
  • Microsoft has Help | Report a Problem, if you don’t know yet. – Lex Li Apr 14 '18 at 04:17
  • @user3163495 , try to put in your virtual directory basic web.config file, wich define razor assemblies, file f.e. http://textuploader.com/du24o – dima_horror Apr 14 '18 at 20:24

1 Answers1

0

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

user3163495
  • 2,425
  • 2
  • 26
  • 43