Could someone please share minimum working ASP.NET Core application project written in F#?
To implement a minimal demo in C#, we have to do the following:
mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new
Then edit project.json
:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
},
"imports": "dnxcore50"
}
}
}
Then perform dotnet restore
and use the follwoing code:
Startup.cs
:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace aspnetcoreapp
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(context =>
{
return context.Response.WriteAsync("Hello from ASP.NET Core!");
});
}
}
}
Program.cs
:
using System;
using Microsoft.AspNetCore.Hosting;
namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Then perform dotnet run
. Could anybody give me a hint how to do the same thing in F#?
I have been exploring new .net core recently and faced the same question. Actually, it's quite easy to do that.
Add F# runtime references into your
project.json
:Then put code below into your
Program.fs
:Just by the way, it's very important to define your Startup class like
type Startup()
nottype Startup
. Otherwise Kestrel runtime will crash during startup.I think what you are looking for is --lang switch.
So the only change to get the base F# project will be:
You can then follow Pavel's answer for the actual ASP.NET logic.
Edit:
By the way,
fsharp
can be also replaced withf#
andfs
, as it is mentioned in the PR.And there is another issue, which proposes changes to
dotnet new
to allow templates.Edit 2:
New tooling supports more templates:
Usage example: