title: Razor The type or namespace could not be found
body: <p>I have a very, very similar problem to <a href="https://github.com/aspnet/Home/issues/1731" rel="nofollow noreferrer">this one</a>.</p>
  
  <blockquote>
    <p>Hi.<br>
    I have an application (Service) targeting .NET 4.61 and trying to move to AspNetCore self host with WebListener.<br>
    I am trying to investigate the requirements by testing it out in a console application.<br>
    The project i crated is a <em>normal Windows console application</em>, i referenced all the required packages with nuget and created required Startup class and create and start the host with WebHostBuilder.</p>
  </blockquote>
  
  <p>(I don't have a normal Windows console application, but a dotnet console application on my ubuntu laptop)</p>
  
  <p>We've differed a little bit in what we've tried and tested before hand.</p>
  
  <p>Up until now I just made a <code>HomeController</code>  and had <code>Index</code> return a <code>string</code> with <code>"Hello world"</code>. Ran the application, went to localhost and got the response. Perfect.</p>
  
  <p>Now all I've added was that <code>Index</code> return an IActionResult by returning <code>View()</code>. Then I made an <code>Index.cshtml</code> file in <code>/Views/Home/</code> and now I get this:</p>
  
  <h2>Stack trace:</h2>
  
  <blockquote>
    <p>fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
          An unhandled exception has occurred: One or more compilation failures occurred:</p>
  </blockquote>
  
  <p>followed by 150 lines like this:</p>
  
  <blockquote>
    <p>gee1fd0w.wuj(4,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)<br>
          gee1fd0w.wuj(5,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)<br>
          gee1fd0w.wuj(6,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)<br>
    ...<br>
    gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Object' is not defined or imported<br>
    gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Void' is not defined or imported  </p>
  </blockquote>
  
  <p>ending with:</p>
  
  <blockquote>
    <p>gee1fd0w.wuj(33,36): error CS0161: '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value<br>
       at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()<br>
       at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath, String normalizedPath, Func'2 compile)<br>
    --- End of stack trace from previous location where exception was thrown ---  </p>
  </blockquote>
  
  <p>Followed by this: </p>
  
  <blockquote>
    <p>at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)<br>
       at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath, Func'2 compile)<br>
       at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)<br>
       at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)<br>
       at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)<br>
       at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)<br>
       at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)<br>
       at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)<br>
       at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)<br>
       at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()<br>
    --- End of stack trace from previous location where exception was thrown ---</p>
  </blockquote>
  
  <p>Followed by a series of shorter stack traces:</p>
  
  <blockquote>
    <p>at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__30.MoveNext()<br>
    --- End of stack trace from previous location where exception was thrown ---<br>
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()<br>
    --- End of stack trace from previous location where exception was thrown ---<br>
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()<br>
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)<br>
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()<br>
    --- End of stack trace from previous location where exception was thrown ---<br>
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)<br>
       at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---<br>
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<br>
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)<br>
       at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()  </p>
  </blockquote>
  
  <h2>End of stack trace</h2>
  
  <p>Now I'm not sure that I fully understand the answer the previous poster got:</p>
  
  <blockquote>
    <p>Mvc's runtime compilation relies on DependencyContext (deps file) to look up compilation references which is only produced when you compile using dotnet CLI. You would have to add compilation references as part of the application startup for view compilation to work.</p>
  </blockquote>
  
  <p>I did compile through dotnet cli, but is there something else that I'm missing here? When I try to replicate my project by running <code>dotnet new mvc</code> and deleting everything but the things that are in my console-project. It runs right as rain and I get hello world from my Index.cshtml.</p>
  
  <p>Instinct tells me that the problem lies in some file-difference, but I've diffed all of the files and I can't find any difference but app-name. I could have missed something but I'm wondering if it could perhaps be something automagical that I'm not aware of?</p>
  
  <h3>EDIT:</h3>
  
  <p>Have found the issue, looks like it's a difference in their generated <code>bin/debug/netcoreapp1.1/&lt;appname&gt;.deps.json</code>. Quite a few differences, going to look into it now. But if I copy paste that file from the mvc-project to the console app, everything runs ok. </p>
  
  <p>Unfortunately the console applications <code>deps.json</code>-file is 1300+ lines long and the mvc applications version is 4300+ lines long so I'm not entirely sure it will be feasible to go through it and identifying why one works and the other don't.</p>
  
  <h3>EDIT:</h3>
  
  <p>It mostly seems to be a whole lot of dependency scopes that differ, a whole lot of the mvc ones contain dependencies to <code>"NETStandard.Library": "1.6.1"</code>, but many others as well. There's also a bunch of compile-scopes that the console-application don't have. </p>
  
  <p>In fact this seems to be the only difference. The mvc-application's <code>deps.json</code>-file seem to have ~3000 more lines of dependencies and compile-scopes. So I think I'll give up the idea of figuring out what's causing the problem and and my plan to learn how to "build a console app into an mvc-app" :)</p>
  
  <p>I'll leave the question here in case anyone find themselves in a similar situation.</p>