F# in ASP.NET Core: possibilities & pitfalls


Using F# with ASP.NET Core allows you to get the best of both worlds: functional programming where it matters and a complete, tried-and-tested web framework. This article will discuss the various ways you could satisfy your urges to use F# while utilizing the ASP.NET Core web framework.

While you could use Suave to build web apps in functional F# really quickly, ASP.NET Core is a larger framework that might be a better fit if you take comfort in knowing your tech stack is being used successfully by other companies (which is, admittedly, a valid concern). Of coarse, that is not Suave’s problem but rather a side effect of the relative rate at which F# is being adopted compared to C#.

When using F# in ASP.NET Core, you’ll come across some problems due to the current state of F# on .NET Core. I’ll try to troubleshoot as much of these in this article so you can smoothly get started.

Your options

There are two immediately obvious ways when using F# with ASP.NET Core:

  1. Create one or more F# libraries & reference them in your C#-based ASP.NET Core project
  2. Create an F#-based ASP.NET Core project and go 100% F#

Frankly choosing one approach over the other is a compromise between having excellent tooling and having a more concise F# codebase.

Option 1: F# libraries + C#-based ASP.NET Core prject

This option is the easiest and in my opinion, the quickest to get up and running with:

  1. Create a C# ASP.NET Core project as you normally would
  2. Write Controllers & Views in C#/Razor normally
  3. Write Models/ViewModels/DTOs in C# (or F#, depending on your taste)
  4. Write business logic/services in F# libraries

Since ASP.NET Core is object-oriented by default, I prefer using C# for Controllers/Models since it’s just easier that way. Usually you’d just download Visual Studio and get started with this - but you’ll run into a couple of issues, listed below.

Don’t use Visual Studio 2017 RC

At the time of writing, Visual F# crashes very easily - I’d wait for the next release where this is fixed. VS 2017 also uses the new MSBuild .NET Core which isn’t yet available for F# (as far as I know). Apparently .NET Core templates for F# will ship with the first major release of VS 2017!

If you need to run on .NET Core (you do!) you have to manually create an F# .NET Core library

The F# Library scaffold in Visual Studio creates a .NET Framework library that’s not cross-platform. You want to be using .NET Core, to do so, you’ll need to use dotnet new --lang F# --type lib to generate the F# .NET Core library.

Don’t use .NET Core 1.1

Sadly, the Current release of .NET Core (1.1) has an F# bug that’ll cause a compiler error. For now, you have to use the LTS release (1.0.1) to get this to work. If you’ve already got multiple versions on your machine you can download the non-installer binaries too.

Option 2: F# libraries + F#-based ASP.NET Core project

There are multiple ways to create a pure ASP.NET Core project in F#:

Personally, I’d go for the yeoman generator (seems to be the most complete) but you might prefer more minimalistic templates. Again, there’s a couple of pitfalls that you might face with this approach too, listed below.

Entity Framework migrations generates C#

So you’ll have to manually port to F# at least once if you opt in for this (database-first), I’m unaware of any methods to make it generate F#.

Incomplete Visual Studio support

If you’re using Visual studio to debug your ASP.NET Core apps, you’ll probably not be able to do the same when using F#. VS Code has perfect support for this, and if you want the same ‘Start without debugging’ feature, I suggest you use dotnet watch to get a similar development cycle where you refresh to see new saved changes appear in the browser.


Using ASP.NET Core with F# is definitely possible, but not without its pitfalls. As I see it, almost all these little annoyances will be gone once the .NET Core ecosystem stabilizes with the release of VS 2017. Do contact me if I’ve made a mistake somewhere since I still don’t have a complete understanding of the intricacies of ASP.NET/.NET Core. I’ve also made a couple of changes to the blog’s color scheme, I’d appreciate feedback on weather that has reduced readability or not.

About Biarity Up Next Article
RSS  Twitter