Skip to content

Add template for empty sitelet project #31

@granicz

Description

@granicz

It is quite often that one needs an empty sitelet to start from. Usually, I perform the following steps to get there (note how the project names tranfers as namespace in the code files):

  1. dotnet new -lang f# -n MyProject

  2. dotnet add package WebSharper and dotnet add package WebSharper.AspNetCore

  3. Add a simple Site.fs as the first code file in the project, with:

    module Site
    
    open WebSharper
    open WebSharper.Sitelets
    
    [<Website>]
    let Main = Application.Text (fun ctx -> "Hello World!")
  4. Merge Program.fs and Startup.fs into a single combined Startup.fs:

    namespace MyProject
    
    open System
    open Microsoft.AspNetCore
    open Microsoft.AspNetCore.Builder
    open Microsoft.AspNetCore.Hosting
    open Microsoft.AspNetCore.Http
    open Microsoft.Extensions.Configuration
    open Microsoft.Extensions.DependencyInjection
    open Microsoft.Extensions.Hosting
    open WebSharper.AspNetCore
    
    type Startup() =
    
        member this.ConfigureServices(services: IServiceCollection) =
            services.AddSitelet(Site.Main)
                .AddAuthentication("WebSharper")
                .AddCookie("WebSharper", fun options -> ())
            |> ignore
    
        member this.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
            if env.IsDevelopment() then app.UseDeveloperExceptionPage() |> ignore
    
            app.UseAuthentication()
                .UseStaticFiles()
                .UseWebSharper()
                .Run(fun context ->
                    context.Response.StatusCode <- 404
                    context.Response.WriteAsync("Page not found"))
    
    module Program =
        let BuildWebHost args =
            WebHost
                .CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build()
    
        [<EntryPoint>]
        let main args =
            BuildWebHost(args).Run()
            0

I believe it would be a benefit to make this a dedicated template, say as websharper-min.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions