Ship Modern .NET Web Apps… A special offer for Blazor Jumpstart (ends soon)

Use ASP.NET Core against .NET 4.6

September 7, 2016 · 3 minute read

If you’re considering using .NET Core for your next web app you might find that your go-to Nuget package isn’t compatible with the framework.

As sh0knah puts it:

There are too many features and third party libraries that we rely on that aren't ready to go yetsh0knah (Reddit)

Does this mean you have to abandon Core and stick to .NET 4.5.x? Well, no!

It is possible to build an ASP.NET Core app and target .NET Framework 4.5.x. That way you get to try out the new web framework but still use your existing libraries and the tried and tested “full fat” .NET Framework.

Target .NET 4.6.1

Bullseye

You can target any version of the .NET framework from 4.5.1 up, you just need to tweak your project.json file.

(If you haven’t already, now’s the time to create a .NET Core web app).

Your project.json file will look something like this.

{
  "version": "1.0.0-*",
  "buildOptions": {
  "debugType": "portable",
  "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
    "dependencies": {
      "Microsoft.NETCore.App": {
        "type": "platform",
        "version": "1.0.0" },
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" },
    "imports": "dnxcore50" }
  }
}

Note that it’s targeting the netcoreapp1.0 framework by default.

Let’s change that to target .NET 4.6.1.

{
  "version": "1.0.0-*",
  "buildOptions": {
  "debugType": "portable",
  "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "net461": {
    "dependencies": {
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
    },
  "imports": "dnxcore50" }
  }
}

As well as targeting the net461 framework, make sure you remove the Microsoft.NETCore.App dependency.

The Microsoft.NETCore.App dependency brings in packages needed for running on .NET Core which aren’t compatible/needed for the full framework.

If you try to run this you’ll get an error.

“Can not find runtime target for framework ‘.NETCoreApp,Version=v1.0’ compatbile with one of the target runtimes:"

This is easily fixed by restoring your app’s dependencies before you run.

dotnet restore
dotnet run

You’ll see a message indicating that your app has been compiled for .NETFramework,Version=v4.6.1.

Hit the site in your browser and you’ll find you can still access your web app as before.

Your app has been compiled to run using the full .NET Framework meaning it will run on any machine that your existing .NET apps run on (including anything you have deployed to production servers/hosted on IIS etc.).

Send in the dependencies

Now you can install your favourite NuGet packages (even if they’ve not been ported to .NET Core).

{
  "version": "1.0.0-*",
  "buildOptions": {
  "debugType": "portable",
  "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "net461": {
    "dependencies": {
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
      "Newtonsoft.Json": "8.0.3"
    },
    "imports": "dnxcore50"
    }
  }
}

In case you’re wondering, Newtonsoft.Json 9.0.1 and above is compatible with .NET Core.

What’s stopping you?

Now you can go forth and learn ASP.NET Core confident in the knowledge you can still use your existing libraries.

photo credit: Bullseye – Arrow on Target via photopin (license)

Join the Practical ASP.NET Newsletter

Ship better Blazor apps, faster. One practical tip every Tuesday.

I respect your email privacy. Unsubscribe with one click.