CSHTML Compilation Error : Cause and Fix

The Context


Recently I had cause to change the name (and output assembly name) of an MVC project, including changing the namespace.  Everything compiled nicely and automated tests ran fine.  However, when I tried to use the application, I received and error something like the one below.

The Problem


Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 26: using System.Web.Optimization;
Line 27: using System.Web.Routing;
Line 28: using ;
Line 29:
Line 30:
Source File: c:\Users\...\AppData\Local\Temp\Temporary ASP.NET Files\root\03cf3763\b07d4eb0\view name.cshtml.a8d08dba.yt1gstrm.0.cs Line: 28

I checked the view and the namespace was not referenced at all, nor anywhere else in the solution code files.  Somehow, somewhere, the original namespace was being referenced.  Eventually I found it.  If you encounter this same error, I hope the solution below is helpful.

The Solution


In the sub-directory 'Views' in your MVC project, there is a web.config file.
Solution /
     Project /
         Views /
           Web.config

Look for the system.web.webPages.razor section and near the bottom there will be a reference to a namespace with the assembly name of the original app.  This will need to change to match the assembly name of the project output (as specified in the application properties).

<xml version="1.0"?>
<configuration>
 <configSections/>
 <system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
       <add namespace="Original.Name.Space" />
  </namespaces>
</pages>
 </system.web.webPages.razor>
 <appSettings/>
 <system.webServer/>   
</configuration>

Comments