Browse Source

Update F# web templates to be more idiomatic

Phillip Carter 5 years ago
parent
commit
c147d57398

+ 1 - 1
src/ProjectTemplates/Web.ProjectTemplates/StarterWeb-FSharp.fsproj.in

@@ -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>

+ 3 - 5
src/ProjectTemplates/Web.ProjectTemplates/content/EmptyWeb-FSharp/Program.fs

@@ -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

+ 6 - 6
src/ProjectTemplates/Web.ProjectTemplates/content/EmptyWeb-FSharp/Startup.fs

@@ -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

+ 12 - 1
src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-FSharp/Controllers/HomeController.fs

@@ -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 })

+ 5 - 4
src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-FSharp/Models/ErrorViewModel.fs

@@ -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))

+ 2 - 1
src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-FSharp/Views/Shared/Error.cshtml

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

+ 14 - 2
src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-FSharp/Controllers/WeatherForecastController.fs

@@ -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 ->

+ 15 - 18
src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-FSharp/Startup.fs

@@ -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
+        app.UseHttpsRedirection()
+           .UseRouting()
+           .UseAuthorization()
+           .UseEndpoints(fun endpoints ->
+                endpoints.MapControllers() |> ignore
+            ) |> ignore
 #else
-
+        app.UseRouting()
+           .UseAuthorization()
+           .UseEndpoints(fun endpoints ->
+                 endpoints.MapControllers() |> ignore
+             ) |> ignore
 #endif
-        app.UseRouting() |> ignore
-
-        app.UseAuthorization() |> ignore
-
-        app.UseEndpoints(fun endpoints ->
-            endpoints.MapControllers() |> ignore
-            ) |> ignore
-
-    member val Configuration : IConfiguration = null with get, set

+ 5 - 5
src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-FSharp/WeatherForecast.fs

@@ -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)