Ser*_*yuk 22 .net f# asp.net-core
有人可以分享用F#编写的最小工作ASP.NET核心应用程序项目吗?
要在C#中实现最小的演示,我们必须执行以下操作:
mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new
Run Code Online (Sandbox Code Playgroud)
然后编辑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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后执行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!");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后执行dotnet run.任何人都可以给我一个提示如何在F#中做同样的事情?
der*_*asp 27
我想你要找的是--lang开关.
因此获得基础F#项目的唯一变化是:
dotnet new --lang fsharp
Run Code Online (Sandbox Code Playgroud)
然后,您可以按照Pavel对实际ASP.NET逻辑的回答.
编辑:
顺便说一句,fsharp也可以替换为f#和fsPR一样.
还有另一个问题,即建议更改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
Run Code Online (Sandbox Code Playgroud)
用法示例:
X:\>dotnet new mvc -n MyFsharpProject -lang F#
Content generation time: 309.5706 ms
The template "MVC ASP.NET Core Web Application" created successfully.
Run Code Online (Sandbox Code Playgroud)
Pav*_* S. 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"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后将下面的代码放入您的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
Run Code Online (Sandbox Code Playgroud)
只是顺便说一下,这是非常重要的定义如启动类type Startup()没有type Startup.否则,Kestrel运行时将在启动期间崩溃.
| 归档时间: |
|
| 查看次数: |
4633 次 |
| 最近记录: |