1

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?

peval27
  • 1,239
  • 2
  • 14
  • 40
  • I made a demo to test , but there is no error thrown .Did you try to update the last version of ASP.NET core 2.2 SDK ? – Xueli Chen Oct 09 '19 at 08:45
  • I'm on that SDK since I want to use VS2017. However I just found the culprit, I write an answer in case somebody else hits the same issue – peval27 Oct 09 '19 at 09:09

2 Answers2

2

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!

peval27
  • 1,239
  • 2
  • 14
  • 40
1

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.

John Meek
  • 173
  • 1
  • 9