Error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

ぃ、小莉子 提交于 2020-03-18 03:12:27

问题


I'm trying to upgrade the following template project to ASP.NET Core 1.1: https://github.com/wilanbigay/aspnet-core-aurelia-typescript-starter

After running dotnet migrate the project.json file has been dropped in favour of the new csproj file.

Using Visual Studio Code and the Nuget4Code extension I've upgraded all components to ASP.NET Core 1.1.

The CsProj now contains entries like so:

<ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk.Web">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.Mvc">
      <Version>1.1.0</Version>
    </PackageReference>

However I have compilation issues. It seems the AspNetCore namespace can't be found. I'm getting the error

Error CS0234: The type or namespace name 'AspNetCore' does not exist in the names pace 'Microsoft' (are you missing an assembly reference?)

I'm not sure how I can check references like I used to be able to in Visual Studio in the references section. How can I resolve this?


回答1:


We just had this issue where visual studio helpfully added a local reference rather than going via nuget

<ItemGroup>
  <Reference Include="Microsoft.AspNetCore.Mvc.Core">
    <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.mvc.core\2.0.0\lib\netstandard2.0\Microsoft.AspNetCore.Mvc.Core.dll</HintPath>
  </Reference>
</ItemGroup>

Removing this and referencing via nuget solved the problem, looks like an issue in Visual Studio 2017.




回答2:


So I guess I was referencing the dependencies but didn't have them installed for the project.

All I needed to do was run dotnet restore

https://docs.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-restore

As stated in the link above this "Restores the dependencies and tools of a project."




回答3:


Mine was simply that I had not referenced Microsoft.AspNetCore.App in the .csproj file.

I added the reference and it worked:

MyTestProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
  ...
</Project>



回答4:


I ran into this problem as well, and in my case the proper packages was installed and all my *.csproj looked correct, but one of the projects in my solution still gave me this error.

A plain dotnet restore didn't do the trick for me. I had to force a reevaluation of all dependencies like so:

dotnet restore --force-evaluate

From the dotnet manual:

Forces restore to reevaluate all dependencies even if a lock file already exists.

A couple of seconds later, my compiler stopped giving me this error.




回答5:


My problem was that a project in the solution was not building properly, so it could not be referenced by other projects in the solution.

The solution was to update Visual Studio (I updated from version 15.5.6 to version 15.9.1). Here is a link to Microsoft's VS Downloads page.




回答6:


Changing

<ItemGroup>
  <Reference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore">
    <HintPath>..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.identity.entityframeworkcore\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Identity.EntityFrameworkCore.dll</HintPath>
  </Reference>
</ItemGroup>

To

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
</ItemGroup>

Solved the issue as @dave-glassborow answered




回答7:


Just add Microsoft.AspNetCore.WebUtilities nuget package




回答8:


I have found this problem when upgrading libraries from .NET Standard 2.0 to 2.1 or .exe's from .NET Core 2.2 to 3.1.

The best bet is just to redo all the NuGet includes for the project from scratch. Open the .csproj file in a text editor, and find the section

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    ...
  </ItemGroup>

and remove all of it, back to

  <ItemGroup>
  </ItemGroup>

then re-open the solution and reinstall all, and only, the required NuGet packages.

This fixes the problem and can help to clean up any unused package cruft as well!



来源:https://stackoverflow.com/questions/41169794/error-cs0234-the-type-or-namespace-name-aspnetcore-does-not-exist-in-the-name

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