Tuesday, July 5, 2016

MVC 5 Razor - Compilation Error CS0246 - Type or namespace could not be found


Problem:

I ran across this issue in a vs 13 project. I was running in my dev environment with a project I had reused. The project was building and running until I removed the web service reference. I followed the prescribed method in MSDN (see reference). Then each time I built the project it compiled. However, each debug session resulted in a runtime error.

Compilation Error

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 'ExpiredNamespace' could not be found (are you missing a using directive or an assembly reference?)





Solution:

I was absolutely puzzled by the error since I had removed all references to the namespace ExpiredNamespace from the project.  It was not reference anywhere.  If I was missing a reference then I should be seeing a compiler error. 


Then I began looking at the trace information and read up on the MVC razor process.  That is when I found my rogue reference.  The error always occurred when the View attempted to run.  I spared a look at the web.config.


<pages pageBaseType="System.Web.Mvc.WebViewPage">

<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="ExpiredNamespace'" />
</namespaces>

</pages>


The base page type renders the on run compilation using these classes.  I had inadvertently carried over the reference in the web.config from my old project!


Note to self, scrub your test projects carefully before reusing them.






Reference:

https://msdn.microsoft.com/en-us/library/d9w023sx.aspx

No comments:

Post a Comment