Scott Hanselman

A Functional Web with ASP.NET Core and F#'s Giraffe

September 18, '17 Comments [26] Posted in DotNetCore
Sponsored By

728331198_7c1854e363_bI was watching Ody Mbegbu's YouTube Channel - it's filled with .NET Core and ASP.NET Tutorial Videos - and was checking out one in particular, "Getting Started with ASP.NET Core Giraffe." Dane Vinson pointed me to it.

There is such a great open source renaissance happening right now with new framework's and libraries popping up in the .NET Core space. I hope you check them out AND support the creators by getting involved, writing docs, filing (kind) issues, and even doing pull requests and fixing bugs or writing tests.

Ody's video was about Dustin Morris' "Giraffe" web framework. Dustin's description is "A native functional ASP.NET Core web framework for F# developers." You can check it out over at

Even better, it uses the "dotnet new" templating system so you can check it out and get started in seconds.

c:> md \mygiraffeeapp & cd \mygiraffeeapp
c:\mygiraffeeapp> dotnet new -i "giraffe-template::*"
c:\mygiraffeeapp> dotnet new giraffe
The template "Giraffe Web App" was created successfully.
c:\mygiraffeeapp> dotnet run
Hosting environment: Production
Content root path: C:\mygiraffeapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Boom. Now I'm checking out Giraffe's "Hello World."

Because ASP.NET Core is very modular and built on "middleware" pipelines, that means that other frameworks like Giraffe can use the bits they want and remove the bits they down. Remembering that this is F#, not C#, here you can see Giraffe adding itself to the pipeline while still using the StaticFileMiddleware.

let configureApp (app : IApplicationBuilder) =
app.UseGiraffeErrorHandler errorHandler
app.UseStaticFiles() |> ignore
app.UseGiraffe webApp

The initial for Giraffe is the docs for now, and frankly, they are excellent and easy to read. The author says:

It is not designed to be a competing web product which can be run standalone like NancyFx or Suave, but rather a lean micro framework which aims to complement ASP.NET Core where it comes short for functional developers. The fundamental idea is to build on top of the strong foundation of ASP.NET Core and re-use existing ASP.NET Core building blocks so F# developers can benefit from both worlds.

Here is a smaller Hello World. Note the use of choose and the clear and terse nature of F#:

open Giraffe.HttpHandlers
open Giraffe.Middleware

let webApp =
choose [
route "/ping" >=> text "pong"
route "/" >=> htmlFile "/pages/index.html" ]

type Startup() =
member __.Configure (app : IApplicationBuilder)
(env : IHostingEnvironment)
(loggerFactory : ILoggerFactory) =

app.UseGiraffe webApp

Is terse an insult? Absolutely not, it's a feature! Check out this single line exampe...and the fish >=> operator! Some people don't like it but I think it's clever.

let app = route "/" >=> setStatusCode 200 >=> text "Hello World"

Making more complex:

let app =
choose [
GET >=> route "/foo" >=> text "GET Foo"
POST >=> route "/foo" >=> text "POST Foo"
route "/bar" >=> text "Always Bar"

Or requiring certain headers:

let app =
mustAccept [ "text/plain"; "application/json" ] >=>
choose [
route "/foo" >=> text "Foo"
route "/bar" >=> json "Bar"

And you can continue to use Razor views as you like, passing in models written in F#

open Giraffe.Razor.HttpHandlers

let model = { WelcomeText = "Hello World" }

let app =
choose [
// Assuming there is a view called "Index.cshtml"
route "/" >=> razorHtmlView "Index" model

There are samples at you can check out as well

* Giraffe photo by Kurt Thomas Hunt, used under CC

Sponsor: A third of teams don’t version control their database. Connect your database to your version control system with SQL Source Control and find out who made changes, what they did, and why. Learn more!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, 18 September 2017 06:37:57 UTC
Thanks for making me aware of this! This will be a great way to introduce F# to my apprentices!
Monday, 18 September 2017 07:50:05 UTC
I wonder what this is all about! A bit complicated I guess
Monday, 18 September 2017 17:30:22 UTC
This looks great, and I might actually be able to use at work. 'dotnet new giraffe' is nice to have, especially getting started. Thanks Scott, will have to spend some time with this soon!
Monday, 18 September 2017 21:48:36 UTC
I've used Suave as an embedded server for a couple of projects and that was a great experience. As Giraffe builds on top of dotnet core though, it can already use a lot of the reusable pieces which are available for the core ecosystem already and of course more of those reusable pieces will become available over the next few months and years. Giraffe seems like a great choice for F# web programming right now.
Stephen Willcock
Monday, 18 September 2017 22:34:54 UTC
small typo, "remove the bits they down" should be "remove the bits they don't"

I wonder if fira code has a ligature for the fish operator....
Tuesday, 19 September 2017 07:46:30 UTC
Sidenote: as someone who hasn't really "gotten" Git yet, it often puzzles me why it's called "Pull Request" instead of what makes more logical sense "Push Request"?

You do a pull to get something out the Git repo right? You push back into the Git repo so others can later pull your changes right? So why isn't this called a "Push Request", i.e. a request for the Git repo maintainers to accept my push of new work?
Tuesday, 19 September 2017 08:11:43 UTC
Hi Scott, wow, thank you for looking at the project and even writing a blog post about it! I just got home from a short trip and this was a great surprise to see today :)

Many thanks to you, Damian, David and the rest of the team for making ASP.NET Core such an amazing web stack!
Tuesday, 19 September 2017 08:28:04 UTC
Yes, Fira code does have a ligature for the fish operator. :)

"Pull request" makes sense when you look from the perspective of the repo owner. Some kind contributor has forked your repo and made some improvement, and now requests that you pull his changes into the master branch. Only approved collaborators are allowed to push changes to your repo.
Tuesday, 19 September 2017 10:57:20 UTC
I have built Botwin, "Botwin is a library that allows Nancy-esque routing for use with ASP.Net Core." Similar to Giraffe in that it's just middleware on top of ASP.Net Core but in C#. Feel free to check it out
Wednesday, 20 September 2017 06:13:44 UTC
"There is such a great open source renaissance happening right now with new framework's and libraries popping up..."

You say reneissance, I say mess ;-)

Seriously, it's quite exhausting trying to keep up with this stuff. Seems like every two days, there's a new way of doing... well, the same thing. Websites. Most of which take some data from a user through a form, and present it as html. But it has to be created using something new.

I don't know. Maybe I'm just too old for this stuff, but sometimes I find it hard to see the reason behind all this stuff. I accept the fact that I may just be boring! :-D
El Dorko
Wednesday, 20 September 2017 14:30:06 UTC
Re: ElDorko
You are correct! So to fix that:

"There is such a great open source MESS happening right now with new framework's and libraries popping up..." :D
... and I like it, as I feel that this mess or "manure" is inevitably letting some new brilliant technologies to grow into beautiful and reliable systems.
Do we have to keep up with all stuff? No.
Will all survive? No
But we can wait 4 years and look back what is grown up and read to be consumed. Someone will blog about what worked and what not.
And we can go out and pick some later on. Or we can stay with good old WIN32/C++/COM :D
Richard Vondráček
Wednesday, 20 September 2017 14:50:07 UTC
Good article, as always. I discover a lot of stuffs here, keep up the good work!
Wednesday, 20 September 2017 20:00:07 UTC works fine. Don't have to use other alternatives
Friday, 22 September 2017 02:21:25 UTC
I see the benefit but, with space being so cheap, is there a specific use case for doing this today? Or is this more a benefit that we will all inherit via Visual Studio in the future?
Friday, 22 September 2017 19:05:07 UTC
Wonderful article!!! I like this website so much it's really awesome. I have also gone through your other posts too and they are also very much appreciate able and I'm just waiting for your next update to come as I like all your posts. . I have bookmarked it and I am looking forward to reading new informative post like this. Thanks for this.
Saturday, 23 September 2017 09:13:00 UTC
I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.
موبايل دست دوم
Monday, 25 September 2017 16:45:41 UTC
good posted

Download Gesturereen to use the unique lock screen with signature.
Monday, 25 September 2017 16:46:31 UTC
nice post
Download Gesturereen to use the unique lock screen with signature.
Tuesday, 26 September 2017 14:05:40 UTC
Mobdro is the one of the best online chatting application Bigo Live pc
bestonline chatting app Download mobdro Best online Movie Player Download Mx player good post
Wednesday, 27 September 2017 17:13:39 UTC
There are so many professional friends here, WOW! It is my pleasure to enjoy this nice article and got so many great friends here:)
Wednesday, 27 September 2017 17:53:42 UTC
Awesome tip! As for what I use, I have several small SlickRun shortcuts with my most common directories. The only issue with this method is that I get a new shell every time, not to change in place. website
Eve More
Wednesday, 27 September 2017 17:53:43 UTC
Awesome tip! As for what I use, I have several small SlickRun shortcuts with my most common directories. The only issue with this method is that I get a new shell every time, not to change in place. website
Eve More
Friday, 29 September 2017 11:22:10 UTC
I think this was the best article ever i saw thank you مانتو اداری
Friday, 29 September 2017 11:24:36 UTC
this is great article thank you مانتو مجلسی
Friday, 29 September 2017 11:26:20 UTC
thank you so much i will subscrible on your site to get more article مانتو بلند
Tuesday, 03 October 2017 05:07:56 UTC

It’s hard to come by experienced people about this subject, but you seem like you know what you’re talking about! Thanks
god images
God Photos
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.