3

Use ASP.NET Core 1.1 with .NET 4.6 (without Visual Studio)

If you want to try out ASP.NET Core but still target the full framework (e.g. 4.x) you can. All you need is a simple tweak to your .csproj file.

In earlier versions of .NET Core this change was made via project.json but Microsoft retired project.json in favour of an MSBuild compatible csproj file.

Why would I want to target .NET 4.x?

.NET Core is still in it’s infancy and you may find your favourite third party library hasn’t been ported over yet.

Or maybe you have existing applications which you still need to interact with and you’re not ready/able to move them over to .NET Core.

If so, you can build a shiny new ASP.NET Core app but still target .NET 4.x and reference other non Core projects.

If you’re using Visual Studio 2017 it’s easy to create a new project targeting the full framework but what if you’re using Visual Studio Code (or another code editor)? You can make the changes yourself.

Targeting .NET 4.x without Visual Studio

You don’t have to use Visual Studio 2017 to write ASP.NET Core apps that target the full framework. You can use this approach to edit/run and debug your code using Visual Studio Code.

First up, make sure you have the latest .NET core SDK installed.

At the time of writing that’s 1.1.

Now create a new mvc app in an empty folder.

dotnet new mvc

Take a look at the folder and you’ll see a *.csproj file.

By default the .csproj file will target a version of .NET Core (either 1.0 or 1.1 depending on your set-up).

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
  </ItemGroup>

</Project>

It’s a simple change to flip this over to target the full framework.

  <PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>

Once you’ve saved that, head back to the console and restore any missing NuGet packages.

dotnet restore

Then run the app.

dotnet run

Now you can reference .NET 4.x packages but write your web app using ASP.NET Core.

photo credit: jrladia Heartland Archery | Winnipeg via photopin (license)

  • Debadutta Pahadsing

    Hi John, Nice Article. I have been able to get aspnet core work on net46 with ef-6/dapper and oracle managed data access provider using only vscode. While trying to debug, I got an error “Only 64-bit processes can be debugged.”, even for simple out-of-the-box aspnet core app, when tfm is net46. If the tfm is netcoreapp1.1, debug works without any problem. Dunno what am I missing? Could you please help? Thanks, Deb

  • John, you are the rockstar today! This is exactly what I needed to be able to use some libraries that have not been updated to .Net Core yet!

  • Mr. Blonde

    This doesnt work if you create >dotnet new angular
    Is there a way to get it to work that way?