gpt4 book ai didi

.net - ASP.NET Core 1.0 F#项目

转载 作者:行者123 更新时间:2023-12-03 11:34:02 24 4
gpt4 key购买 nike

有人可以分享用F#编写的最低工作ASP.NET Core应用程序项目吗?

要在C#中实现最小演示,我们必须执行以下操作:

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并使用以下代码:
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 run。有人可以提示我如何在F#中执行相同的操作吗?

最佳答案

我最近一直在探索新的.net核心,并且遇到了同样的问题。实际上,这样做很容易。

将F#运行时引用添加到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中:
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运行时将在启动期间崩溃。

关于.net - ASP.NET Core 1.0 F#项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com