Update F# web templates to be more idiomatic

This commit is contained in:
Phillip Carter 2020-05-16 11:18:44 -07:00
parent ff773bd843
commit c147d57398
9 changed files with 64 additions and 44 deletions

View File

@ -7,8 +7,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Controllers/HomeController.fs" />
<Compile Include="Models/ErrorViewModel.fs" />
<Compile Include="Controllers/HomeController.fs" />
<Compile Include="Startup.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

View File

@ -12,9 +12,7 @@ open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
module Program =
let exitCode = 0
let CreateHostBuilder args =
let createHostBuilder args =
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(fun webBuilder ->
webBuilder.UseStartup<Startup>() |> ignore
@ -22,6 +20,6 @@ module Program =
[<EntryPoint>]
let main args =
CreateHostBuilder(args).Build().Run()
createHostBuilder(args).Build().Run()
exitCode
0 // Exit code

View File

@ -11,16 +11,16 @@ type Startup() =
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
member this.ConfigureServices(services: IServiceCollection) =
member _.ConfigureServices(services: IServiceCollection) =
()
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
member _.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
if env.IsDevelopment() then
app.UseDeveloperExceptionPage() |> ignore
app.UseRouting() |> ignore
app.UseEndpoints(fun endpoints ->
endpoints.MapGet("/", fun context -> context.Response.WriteAsync("Hello World!")) |> ignore
app.UseRouting()
.UseEndpoints(fun endpoints ->
endpoints.MapGet("/", fun context ->
context.Response.WriteAsync("Hello World!")) |> ignore
) |> ignore

View File

@ -4,9 +4,13 @@ open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open System.Diagnostics
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Logging
open Company.WebApplication1.Models
type HomeController (logger : ILogger<HomeController>) =
inherit Controller()
@ -16,5 +20,12 @@ type HomeController (logger : ILogger<HomeController>) =
member this.Privacy () =
this.View()
[<ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)>]
member this.Error () =
this.View();
let reqId =
if isNull Activity.Current then
this.HttpContext.TraceIdentifier
else
Activity.Current.Id
this.View({ RequestId = reqId })

View File

@ -1,8 +1,9 @@
namespace Company.WebApplication1
namespace Company.WebApplication1.Models
open System
type ErrorViewModel private () =
member val RequestId : string = null with get, set
type ErrorViewModel =
{ RequestId: string }
member val ShowRequestId : bool = true with get, set
member this.ShowRequestId =
not (String.IsNullOrEmpty(this.RequestId))

View File

@ -1,4 +1,5 @@
@model ErrorViewModel
@using Company.WebApplication1.Models
@model ErrorViewModel
@{
ViewData["Title"] = "Error";
}

View File

@ -13,10 +13,22 @@ open Company.WebApplication1
type WeatherForecastController (logger : ILogger<WeatherForecastController>) =
inherit ControllerBase()
let summaries = [| "Freezing"; "Bracing"; "Chilly"; "Cool"; "Mild"; "Warm"; "Balmy"; "Hot"; "Sweltering"; "Scorching" |]
let summaries =
[|
"Freezing"
"Bracing"
"Chilly"
"Cool"
"Mild"
"Warm"
"Balmy"
"Hot"
"Sweltering"
"Scorching"
|]
[<HttpGet>]
member __.Get() : WeatherForecast[] =
member _.Get() =
let rng = System.Random()
[|
for index in 0..4 ->

View File

@ -14,32 +14,29 @@ open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
type Startup private () =
new (configuration: IConfiguration) as this =
Startup() then
this.Configuration <- configuration
type Startup(configuration: IConfiguration) =
member _.Configuration = configuration
// This method gets called by the runtime. Use this method to add services to the container.
member this.ConfigureServices(services: IServiceCollection) =
member _.ConfigureServices(services: IServiceCollection) =
// Add framework services.
services.AddControllers() |> ignore
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
member _.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
if (env.IsDevelopment()) then
app.UseDeveloperExceptionPage() |> ignore
#if (!NoHttps)
app.UseHttpsRedirection() |> ignore
#else
#endif
app.UseRouting() |> ignore
app.UseAuthorization() |> ignore
app.UseEndpoints(fun endpoints ->
endpoints.MapControllers() |> ignore
app.UseHttpsRedirection()
.UseRouting()
.UseAuthorization()
.UseEndpoints(fun endpoints ->
endpoints.MapControllers() |> ignore
) |> ignore
member val Configuration : IConfiguration = null with get, set
#else
app.UseRouting()
.UseAuthorization()
.UseEndpoints(fun endpoints ->
endpoints.MapControllers() |> ignore
) |> ignore
#endif

View File

@ -3,9 +3,9 @@ namespace Company.WebApplication1
open System
type WeatherForecast =
{ Date: DateTime
TemperatureC: int
Summary: string }
{ Date: DateTime
TemperatureC: int
Summary: string }
member this.TemperatureF =
32 + (int (float this.TemperatureC / 0.5556))
member this.TemperatureF =
32.0 + (float this.TemperatureC / 0.5556)