0. The quick fix
As already noted in the currently highest voted answer,the quick fix is to use the package manager, Tools> Nuget PackageManager> Package Manager Console, to run
Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r
1. Code to reproduce the error
Here is code that reproduces the error:
https://user.it.uu.se/%7Ehesc0353/SrvrErr-reproduce.zip
(Originally fromhttps://github.com/aspnet/AspNetDocs/tree/master/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client/sample/server/ProductsApp)
Consider trying the example code provided in the zip file above.
If no changes are made, Ctrl+F5 will reproducethe error.
2. A more robust solution
An alternative solution is to remove an attribute from the project'sWeb.config
file.
(Web.config
is in the same directory as the .csproj
file.)
This will automatically and silently recreate your packages if theyare missing.
Open the Web.config
file in a text editor or in Visual Studio.
<?xml version="1.0" encoding="utf-8"?><configuration><appSettings></appSettings> ...<system.codedom><compilers><compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/><compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/></compilers></system.codedom></configuration>
In the tag configuration> system.codedom> compilers>compiler language="c#;cs;csharp", completely removethe type
attribute.– In short, remove the line that starts withtype="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,
.1
Visual Studio will take care of the rest.– No more Server Error in '/' Application
.
3. HTTP Error 403
In the example provided above, hitting Ctrl+F5will now result in an HTTP Error 403.
Try replacing http://localhost:64195
in your web browser withhttp://localhost:64195/api/products
.The web API now displays correctly:
As a provocation, I tried removing the whole package
directory fromthe Visual Studio project.
It was automatically and silently recreated as soon as the project wasrebuilt.
References
1 Presumably, the same fix works for Visual Basic as wellas for C#, but I haven't tried it.