Search code examples
angulardotnet-cli

Convert a "dotnet new Angular" project to net461?


I have been playing around with the project created via

dotnet new Angular.

I love it!

But I need to reference a .net 4.6.1 project from my project. My first thought was to change the Angular project into a .net 4.6.1 project, as one 461 project can obviously reference another such. So I tried changing the .csproj file, replacing

<TargetFramework>netcoreapp2.0</TargetFramework>

with

<TargetFramework>net461</TargetFramework>.

But then I get a bunch of build errors, starting with:

Package Microsoft.AspNetCore.All 2.0.0 is not compatible with net461 (.NETFramework,Version=v4.6.1) / win7-x86. Package Microsoft.AspNetCore.All 2.0.0 supports: netcoreapp2.0 (.NETCoreApp,Version=v2.0)   Angular C:\Code\Learning\angular\foo\AngularTest\Angular\Angular.csproj 1   

If I remove the reference to Microsoft.AspNetCore.All, I get errors from the code, saying that Microsoft.AspNetCore.All is missing.

I've used Angular with .net 4.6.1 before without problems. So there ought to be a way to do this. But what is it?


Solution

  • You can target net461 in the Angular template, but you have to list the referenced assemblies separately in the .csproj file, rather than use the Microsoft.AspNetCore.All.

    It will look something like this toward the top of the file.

    <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
        <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
        <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
        <IsPackable>false</IsPackable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
        <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.NodeServices" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
        <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
      </ItemGroup>
    

    I just did the switch myself, and it worked.