VS2017生成MVC元素报错‘Sequence contains no elements’求助
Hey there! Let's dig into this annoying issue you're hitting with the ASP.NET MVC code generator. The fact that it works fine at home but fails consistently at your office is a huge clue—let's break down the most likely causes and how to check them:
1. Network/Proxy/Firewall Restrictions (Most Probable Cause)
The MVC scaffolding tool (used to generate views/controllers) often relies on accessing online resources like NuGet packages or Microsoft's code generation templates. If your company network has strict firewall rules, proxy settings, or blocks external package sources like NuGet.org, the generator can't fetch the necessary components it needs to run. When it can't find these resources, it throws the vague "Sequence contains no elements" error because it's expecting a list of templates/dependencies that never loads.
To test this:
- Try manually accessing NuGet.org in your office browser—if it's blocked, that's a clear sign.
- In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Settings and check your package sources. If your company uses an internal NuGet feed, make sure it includes all the required scaffolding packages (like
Microsoft.VisualStudio.Web.CodeGeneration.Design). - Attempt to restore NuGet packages for your project (right-click project > Restore NuGet Packages). If you get errors about unable to reach package sources, network restrictions are the culprit.
2. Incomplete/Inconsistent Project Dependencies
It's possible that your office copy of the project is missing critical packages needed for code generation, while your home version has them properly installed. The scaffolding tool depends on specific packages to function, and if they're missing or mismatched, it can fail unexpectedly.
Check these:
- Open your project's
.csprojfile and verify the presence of theMicrosoft.VisualStudio.Web.CodeGeneration.Designpackage. If it's missing, install it via the Package Manager Console with:Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design - Compare the package versions between your home and office projects—mismatched versions can cause compatibility issues. Ensure they're identical.
3. Visual Studio/Code Generator Tool Version Mismatches
Your office installation of Visual Studio or the global .NET code generation tool might be outdated or misconfigured compared to your home setup.
- Check the global
dotnet-aspnet-codegeneratortool version: Open a command prompt and run:
If it's missing or outdated, reinstall it with:dotnet tool list -gdotnet tool install -g dotnet-aspnet-codegenerator - Ensure your office Visual Studio is up to date with the latest updates—sometimes old versions have known bugs with scaffolding.
Quick Workaround (Until You Fix the Root Cause)
While copying views manually works temporarily, it's not ideal because scaffolding often sets up related controller logic, routing, and other boilerplate that you might miss. If you need to keep working while troubleshooting, you can try generating the views/controllers at home, commit them to your repo, and pull them into your office project—this avoids running the generator in the restricted network environment.
内容的提问来源于stack exchange,提问作者Patrick Christensen

