ASP.NET Core 1.0 F# 项目

编程入门 行业动态 更新时间:2024-10-27 05:24:11
本文介绍了ASP.NET Core 1.0 F# 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以分享用 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 run.谁能给我一个提示,如何在 F# 中做同样的事情?

Then perform dotnet run. Could anybody give me a hint how to do the same thing in F#?

推荐答案

我最近一直在探索新的 核心并面临同样的问题.其实,这很容易做到.

I have been exploring new 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.Corecore": "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

顺便说一句,像type Startup()而不是type Startup那样定义你的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# 项目

本文发布于:2023-11-05 07:20:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!