peval27
peval27

Reputation: 1309

Razor fails to render view due to weird compilation error CS0234

I have an existing ASP.NET core 2.2 (SDK version 2.2.108) Web API and I'm now adding MVC to host a web page with a controller. I've added: - the new controller - the new .cshtml view - made sure my startup.cs contains services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

I'm using Http.Sys and I get 500 [Microsoft.AspNetCore.Server.HttpSys.MessagePump] ProcessRequestAsync. If I enable exceptions in VS 2017 (15.9.13) I see:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: 'One or more compilation failures occurred: 310z1p4f.mzl(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?) 310z1p4f.mzl(5,40): error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?) 310z1p4f.mzl(4,82): error CS0518: Predefined type 'System.Type' is not defined or imported 310z1p4f.mzl(4,122): error CS0518: Predefined type 'System.String' is not defined or imported

and it continues.

I see in the build output:

Microsoft.AspNetCore.Razor.dll
Microsoft.AspNetCore.Razor.Language.dll
Microsoft.AspNetCore.Razor.Runtime.dll

I followed several different discussions on SO and github:

but no luck. Ideas?

Upvotes: 1

Views: 1642

Answers (2)

peval27
peval27

Reputation: 1309

Ok, I found the issue. Basically, there are two projects:

  1. a console app which can be also run as a Windows service . This instantiate the web server
  2. a library which contains the Web API

Both were targeting <Project Sdk="Microsoft.NET.Sdk">, once I've changed 2 to use <Project Sdk="Microsoft.NET.Sdk.Web"> and to be a library not console app, everything started working.

It took me a long time to figure this out, since the Web API was working fine before with that (wrong) configuration. meh!

Upvotes: 2

John Meek
John Meek

Reputation: 181

Ok, Try this right click your project and edit your project file.

under project reference you should see

<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" 
    PrivateAssets="All" />

if you do not add it.

Upvotes: 1

Related Questions