有人可以分享用F#编写的最小工作ASP.NET核心应用程序项目吗?
要在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#中做同样的事情吗?
答案 0 :(得分:27)
我认为你要找的是--lang切换。
因此获得基础F#项目的唯一变化是:
dotnet new --lang fsharp
然后,您可以按照Pavel对实际ASP.NET逻辑的回答。
修改强>:
顺便说一句,fsharp
也可以替换为f#
和fs
,正如公关中提到的那样。
还有另一个issue,它建议对dotnet new
进行更改以允许模板。
编辑2 :
新工具支持更多模板:
Templates Short Name Language Tags
--------------------------------------------------------------------------------------
Console Application console [C#], F# Common/Console
Class library classlib [C#], F# Common/Library
Unit Test Project mstest [C#], F# Test/MSTest
xUnit Test Project xunit [C#], F# Test/xUnit
Empty ASP.NET Core Web Application web [C#] Web/Empty
MVC ASP.NET Core Web Application mvc [C#], F# Web/MVC
Web API ASP.NET Core Web Application webapi [C#] Web/WebAPI
Solution File sln Solution
用法示例:
X:\>dotnet new mvc -n MyFsharpProject -lang F#
Content generation time: 309.5706 ms
The template "MVC ASP.NET Core Web Application" created successfully.
答案 1 :(得分:21)
我最近一直在探索新的.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运行时将在启动期间崩溃。