Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to start creating a Giraffe web project and how to serve it?

I have to keep reminding myself that a Giraffe project plugs into the ASP.NET Core pipeline or is itself an ASP.NET Core application , so if I can't find answers to my questions in the Giraff...

posted 2mo ago by toraritte‭

Answer
#1: Initial revision by user avatar toraritte‭ · 2024-03-27T14:41:25Z (about 2 months ago)
I have to keep reminding myself that

> a Giraffe project plugs into the [ASP.NET Core][1] pipeline or is itself an [ASP.NET Core][1] application

, so if I can't find answers to my questions in the [Giraffe docs][2], then it is probably because it is an [ASP.NET Core][1] topic (or an F# / .NET / etc. one).
 
### How to create and serve a Giraffe project

Steps 0. to 5. follow the [Get started with F# with command-line tools (.NET | Microsoft Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line) article.

0. (_OPTIONAL_) **Create a new solution**.

   ```
   dotnet new sln -o SampleSolution
   ```

1. **Enter the solution's directory**.

   ```
   cd SampleSolution
   ```

2. **Create an empty [ASP.NET Core][1] project**.

   ```
   dotnet new web -lang "F#" -o src/GiraffeWebExample
   ```
   
   > INFO The available `dotnet new` templates are available on the links below. (Both seem to list them all, but not sure which one is more up-to-date.)
   > + \[Microsoft Learn]\[.NET CLI] [.NET default templates for `dotnet new`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new-sdk-templates)
   > + \[Microsoft Learn]\[.NET CLI] [`dotnet new <TEMPLATE>`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new)


3. (_OPTIONAL_) **Add new project to solution**.

   ```
   dotnet sln add src/GiraffeWebExample/GiraffeWebExample.fsproj
   ```

4. **Enter the project's directory**.

   ```
   cd src/GiraffeWebExample/
   ```

5. **Install dependencies**.

   ```
   dotnet add package Microsoft.AspNetCore.App
   dotnet add package Giraffe
   ```

   > NOTE I got a warning below when adding Giraffe, so just pasting it here for completeness' sake:
   > ```
   > /usr/local/share/dotnet/sdk/8.0.202/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.DefaultItems.Shared.targets(111,5):
   > warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not
   > necessary when targeting .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web
   > is used, the shared framework will be referenced automatically. Otherwise,
   > the PackageReference should be replaced with a FrameworkReference. 
   > [/Users/toraritte/dev/shed/dotnet/giraffe/ByHand/src/ByHand/ByHand.fsproj]
   > ```

6. **Add the "entry point"**.

   > NOTE Still haven't figured out what other ways .NET has to set up a web project, but the `EntryPoint` attribute is covered in the [[Microsoft Learn][F# Guide] Console Applications and Explicit Entry Points](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/entry-point) article.


   I chose to simply copy one of the sample codes from the [Doing it manually](https://giraffe.wiki/#doing-it-manually) section; I prefer the more functional approach, so here it is the second one:

   ```
   open System
   open Microsoft.AspNetCore.Builder
   open Microsoft.AspNetCore.Hosting
   open Microsoft.Extensions.Hosting
   open Microsoft.Extensions.DependencyInjection
   open Giraffe

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

   let configureApp (app : IApplicationBuilder) =
       // Add Giraffe to the ASP.NET Core pipeline
       app.UseGiraffe webApp

   let configureServices (services : IServiceCollection) =
       // Add Giraffe dependencies
       services.AddGiraffe() |> ignore

   [<EntryPoint>]
   let main _ =
       Host.CreateDefaultBuilder()
           .ConfigureWebHostDefaults(
               fun webHostBuilder ->
                   webHostBuilder
                       .Configure(configureApp)
                       .ConfigureServices(configureServices)
                       |> ignore)
           .Build()
           .Run()
       0
   ```

7. **Run / serve project**.

   ```
   dotnet watch run
   ```

   > INFO Started with the [Get started with ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/getting-started/) article in the ASP.NET Core docs.


  [1]: https://learn.microsoft.com/en-us/aspnet/core/
  [2]: https://giraffe.wiki/docs