ASP.NET Core Scaffolding does not work in VS 2017

余生长醉 提交于 2020-12-08 05:36:47

问题


I have a beginner ASP.NET Core project in Visual Studio 2017, and I am at the scaffolding step of the HelloWorld. The Scaffolding does not work, I tested on a first computer, then at a second one...

when I try to generate a controller with views , it gives the following error on the first machine:

Microsoft Visual Studio


Error

There was an error running the selected code generator:

'Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. at Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args)'


OK

On the second machine:

Microsoft Visual Studio


Error

There was an error running the selected code generator:

'Error: assembly specified in the dependencies manifest was not found -- package: 'microsoft.applicationinsights.aspnetcore', version: '2.1.0', path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll''


OK


回答1:


The following works for me.

I deleted bin & obj folders, build the project again and it works.

In case if it gives you error: it couldn't find project.deps.json in debug folder.

Set project to debug, ran it and now you can add scaffolded items.




回答2:


Clear local NuGet cache with the command from console:

dotnet nuget locals all --clear

Restore all Packages

Install (or update) NuGet package Microsoft.CodeAnalysis.CSharp.Workspaces version 2.0.0 (or higher) EDIT: It seams versions 2.3.0 and 2.3.1 cause some problems. So stick with 2.2.0 at most until MS solves this issues (https://github.com/dotnet/roslyn/issues/20873).

Restore all Packages again and try scaffolding.

That worked for me.

Be sure you have properly referenced these packages in your project:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />

<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />

My projects uses Asp.Net Core 1.1.2 and VS2017 Community




回答3:


I has the same situation. On .net core version 2.2 and with VS 2017 Enterprise.

The next instruction hepls for me:

  • open VS with Admin rights.
  • load solution to VS
  • add controller from scaffolding menu. VS will install 'Microsoft.VisualStudio.Web.CodeGeneration.Design' and 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' packages.
  • close VS
  • open VS and load project. Add controllers via scaffloading is working fine

Maybe this instruction helps somebody, Thanks.




回答4:


For the benefit of searchers:

I got this in VS2019. The cause was that my code was broken.

Didn't realise why to start, but when you realise that code generation is a nuget package; if the project doesn't build, then Visual Studio can't get to that package.

Once I got the code compiling, this error went away.




回答5:


you should change the version of Microsoft.VisualStudio.Web.CodeGeneration.Design exactly like the version of Microsoft.AspNetCore.App




回答6:


I was trying to run the command bellow with the version 3.0.0 of asp.net core installed, so I got the same error Command: dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

Error: Scaffolding failed. Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

  • Solution:

dotnet nuget locals all --clear

dotnet remove package Microsoft.VisualStudio.Web.CodeGeneration.Utils

Change the PackageRefere for the version 2.2.0

Run the command again. dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries




回答7:


Solution:

  1. Go to VS2019 --> Dependencies -->Packages
  2. Click on Manage NuGet
  3. In the search type "Microsoft.VisualStudio.Web.CodeGeneration.Utils" or the Dll that is missing.
  4. Click Install.
  5. Rebuild the solution
  6. Then add the Scaffolding item. It works..:-)


来源:https://stackoverflow.com/questions/45139243/asp-net-core-scaffolding-does-not-work-in-vs-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!