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.
How to start creating a Giraffe web project and how to serve it?
Having some trouble with giraffe-template
on Mac M1, so decided to set up a Giraffe project manually. Started following the Doing it manually section of the Giraffe README, but got stuck right away, and I also couldn't see mentioned anywhere how the project could be served.
For the record, the Giraffe docs are great. I'm new to .NET, so the parts I'm struggling with are the basics of .NET project management, F#, and ASP.NET Core - it would be unreasonable to expect these topics covered in there.
1 answer
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 Giraffe docs, then it is probably because it is an ASP.NET Core 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) article.
-
(OPTIONAL) Create a new solution.
dotnet new sln -o SampleSolution
-
Enter the solution's directory.
cd SampleSolution
-
Create an empty ASP.NET Core 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
- [Microsoft Learn][.NET CLI]
dotnet new <TEMPLATE>
- [Microsoft Learn][.NET CLI] .NET default templates for
-
(OPTIONAL) Add new project to solution.
dotnet sln add src/GiraffeWebExample/GiraffeWebExample.fsproj
-
Enter the project's directory.
cd src/GiraffeWebExample/
-
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]
-
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 article.I chose to simply copy one of the sample codes from the 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
-
Run / serve project.
dotnet watch run
INFO Started with the Get started with ASP.NET Core article in the ASP.NET Core docs.
0 comment threads