Scott Hanselman

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

September 18, 2017 Comment on this post [11] 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 https://github.com/dustinmoris/Giraffe.

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 readme.md 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 https://github.com/dustinmoris/Giraffe/tree/master/samples 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
Hosting By
Hosted in an Azure App Service
September 18, 2017 10:37
Thanks for making me aware of this! This will be a great way to introduce F# to my apprentices!
September 18, 2017 21:30
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!
September 19, 2017 1:48
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 asp.net 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.
September 19, 2017 2:34
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....
September 19, 2017 11:46
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?
September 19, 2017 12:11
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!
September 19, 2017 12:28
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.
September 19, 2017 14:57
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 https://github.com/jchannon/botwin
September 20, 2017 10:13
"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
September 20, 2017 18:30
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
September 21, 2017 0:00
ASP.net works fine. Don't have to use other alternatives

Comments are closed.

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