Update F# web templates to be more idiomatic (#21912)

* Update F# web templates to be more idiomatic
This commit is contained in:
Pranav K 2020-05-20 11:21:55 -07:00 committed by GitHub
commit c14fb30b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 40 deletions

View File

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

View File

@ -12,9 +12,7 @@ open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging open Microsoft.Extensions.Logging
module Program = module Program =
let exitCode = 0 let createHostBuilder args =
let CreateHostBuilder args =
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(fun webBuilder -> .ConfigureWebHostDefaults(fun webBuilder ->
webBuilder.UseStartup<Startup>() |> ignore webBuilder.UseStartup<Startup>() |> ignore
@ -22,6 +20,6 @@ module Program =
[<EntryPoint>] [<EntryPoint>]
let main args = 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. // 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 // 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. // 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 if env.IsDevelopment() then
app.UseDeveloperExceptionPage() |> ignore app.UseDeveloperExceptionPage() |> ignore
app.UseRouting() |> ignore app.UseRouting()
.UseEndpoints(fun endpoints ->
app.UseEndpoints(fun endpoints -> endpoints.MapGet("/", fun context ->
endpoints.MapGet("/", fun context -> context.Response.WriteAsync("Hello World!")) |> ignore context.Response.WriteAsync("Hello World!")) |> ignore
) |> ignore ) |> ignore

View File

@ -4,9 +4,13 @@ open System
open System.Collections.Generic open System.Collections.Generic
open System.Linq open System.Linq
open System.Threading.Tasks open System.Threading.Tasks
open System.Diagnostics
open Microsoft.AspNetCore.Mvc open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Logging open Microsoft.Extensions.Logging
open Company.WebApplication1.Models
type HomeController (logger : ILogger<HomeController>) = type HomeController (logger : ILogger<HomeController>) =
inherit Controller() inherit Controller()
@ -16,5 +20,12 @@ type HomeController (logger : ILogger<HomeController>) =
member this.Privacy () = member this.Privacy () =
this.View() this.View()
[<ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)>]
member this.Error () = 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 open System
type ErrorViewModel private () = type ErrorViewModel =
member val RequestId : string = null with get, set { 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"; ViewData["Title"] = "Error";
} }

View File

@ -13,10 +13,22 @@ open Company.WebApplication1
type WeatherForecastController (logger : ILogger<WeatherForecastController>) = type WeatherForecastController (logger : ILogger<WeatherForecastController>) =
inherit ControllerBase() 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>] [<HttpGet>]
member __.Get() : WeatherForecast[] = member _.Get() =
let rng = System.Random() let rng = System.Random()
[| [|
for index in 0..4 -> for index in 0..4 ->

View File

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

View File

@ -8,4 +8,4 @@ type WeatherForecast =
Summary: string } Summary: string }
member this.TemperatureF = member this.TemperatureF =
32 + (int (float this.TemperatureC / 0.5556)) 32.0 + (float this.TemperatureC / 0.5556)