ASP.NET Core 1.0 F#项目 [英] ASP.NET Core 1.0 F# project
问题描述
有人可以分享用F#编写的最低工作ASP.NET Core应用程序项目吗?
Could someone please share minimum working ASP.NET Core application project written in F#?
要在C#中实现最小演示,我们必须执行以下操作:
To implement a minimal demo in C#, we have to do the following:
mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new
然后编辑 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"
}
}
}
然后执行 dotnet restore
并使用以下代码:
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();
}
}
}
然后执行 dotnet运行
。有人可以提示我如何在F#中执行相同的操作吗?
Then perform dotnet run
. Could anybody give me a hint how to do the same thing in F#?
推荐答案
我最近一直在探索新的.net核心,面对同样的问题。
I have been exploring new .net core recently and faced the same question. Actually, it's quite easy to do that.
将F#运行时引用添加到 project.json
:
Add F# runtime references into your project.json
:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"compilerName": "fsc",
"compile": "**/*.fs"
},
"dependencies": {
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160509",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
},
"tools": {
"dotnet-compile-fsc": {
"version": "1.0.0-preview2-*",
"imports": [
"dnxcore50",
"portable-net45+win81",
"netstandard1.3"
]
}
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"portable-net45+win8",
"dnxcore50"
]
}
}
}
然后将下面的代码放入您的 Program.fs
:
Then put code below into your Program.fs
:
open System
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
type Startup() =
member this.Configure(app: IApplicationBuilder) =
app.Run(fun context -> context.Response.WriteAsync("Hello from ASP.NET Core!"))
[<EntryPoint>]
let main argv =
let host = WebHostBuilder().UseKestrel().UseStartup<Startup>().Build()
host.Run()
printfn "Server finished!"
0
顺便说一句,定义您的Startup类(如 type Startup()
不是 type Startup
。否则,Kestrel运行时将在启动期间崩溃。
Just by the way, it's very important to define your Startup class like type Startup()
not type Startup
. Otherwise Kestrel runtime will crash during startup.
这篇关于ASP.NET Core 1.0 F#项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!