Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the sample #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
###############################
# Core EditorConfig Options #
###############################
root = true
# All files
[*]
indent_style = space

# XML project files
[*.{csproj,fsproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

# XML config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2

# Code files
[*.{cs,csx,fs,fsx}]
indent_size = 4
insert_final_newline = true
charset = utf-8
74 changes: 53 additions & 21 deletions Giraffe.Razor.sln
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{03CCBA43-B6F5-4FA7-8C18-432BB07225FC}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Razor", "src\Giraffe.Razor\Giraffe.Razor.fsproj", "{9B52367B-CC80-452E-9062-7583995BFAC0}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Giraffe.Razor", "src\Giraffe.Razor\Giraffe.Razor.fsproj", "{9B52367B-CC80-452E-9062-7583995BFAC0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E067A6B8-538E-4362-83C2-9ECBC486AC31}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "GiraffeRazorSample", "samples\GiraffeRazorSample\GiraffeRazorSample.fsproj", "{14818084-9969-4F42-8A66-394FC6670B50}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "GiraffeRazorSample", "samples\GiraffeRazorSample\GiraffeRazorSample.fsproj", "{14818084-9969-4F42-8A66-394FC6670B50}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_root", "_root", "{73E09F74-F64D-4C42-92FC-19731C0C5553}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
.gitattributes = .gitattributes
LICENSE = LICENSE
NuGet.config = NuGet.config
README.md = README.md
RELEASE_NOTES.md = RELEASE_NOTES.md
giraffe-64x64.png = giraffe-64x64.png
CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md
EndProjectSection
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md
giraffe-64x64.png = giraffe-64x64.png
LICENSE = LICENSE
NuGet.config = NuGet.config
README.md = README.md
RELEASE_NOTES.md = RELEASE_NOTES.md
EndProjectSection
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Models", "samples\Models\Models.fsproj", "{7FF78E51-A357-4D4B-90FF-B03E148AE18C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorClassLibrary", "samples\RazorClassLibrary\RazorClassLibrary.csproj", "{0A429935-F750-4334-98BE-9FC26764A174}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -32,13 +37,6 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9B52367B-CC80-452E-9062-7583995BFAC0} = {03CCBA43-B6F5-4FA7-8C18-432BB07225FC}
{14818084-9969-4F42-8A66-394FC6670B50} = {E067A6B8-538E-4362-83C2-9ECBC486AC31}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9B52367B-CC80-452E-9062-7583995BFAC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B52367B-CC80-452E-9062-7583995BFAC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -64,5 +62,39 @@ Global
{14818084-9969-4F42-8A66-394FC6670B50}.Release|x64.Build.0 = Release|Any CPU
{14818084-9969-4F42-8A66-394FC6670B50}.Release|x86.ActiveCfg = Release|Any CPU
{14818084-9969-4F42-8A66-394FC6670B50}.Release|x86.Build.0 = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|x64.ActiveCfg = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|x64.Build.0 = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|x86.ActiveCfg = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Debug|x86.Build.0 = Debug|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|Any CPU.Build.0 = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|x64.ActiveCfg = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|x64.Build.0 = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|x86.ActiveCfg = Release|Any CPU
{7FF78E51-A357-4D4B-90FF-B03E148AE18C}.Release|x86.Build.0 = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|x64.ActiveCfg = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|x64.Build.0 = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|x86.ActiveCfg = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Debug|x86.Build.0 = Debug|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|Any CPU.Build.0 = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|x64.ActiveCfg = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|x64.Build.0 = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|x86.ActiveCfg = Release|Any CPU
{0A429935-F750-4334-98BE-9FC26764A174}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9B52367B-CC80-452E-9062-7583995BFAC0} = {03CCBA43-B6F5-4FA7-8C18-432BB07225FC}
{14818084-9969-4F42-8A66-394FC6670B50} = {E067A6B8-538E-4362-83C2-9ECBC486AC31}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D44B626-1BB8-4B87-8E9F-045072A0598C}
EndGlobalSection
EndGlobal
19 changes: 6 additions & 13 deletions samples/GiraffeRazorSample/GiraffeRazorSample.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>GiraffeRazorSample</AssemblyName>
<PackageId>GiraffeRazorSample</PackageId>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
Expand All @@ -10,24 +10,17 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Giraffe.Razor\Giraffe.Razor.fsproj" />
<ProjectReference Include="..\Models\Models.fsproj" />
<ProjectReference Include="..\RazorClassLibrary\RazorClassLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Ply" Version="0.3.*" />
</ItemGroup>

<ItemGroup>
<Watch Include="**\*.cshtml" Exclude="bin\**\*" />
<None Include="Properties\launchSettings.json" />
<Content Include="wwwroot\main.css" />
</ItemGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<Content Include="Views\*.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
52 changes: 28 additions & 24 deletions samples/GiraffeRazorSample/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,9 @@ open Microsoft.AspNetCore.Http.Features
open Microsoft.AspNetCore.Mvc.ModelBinding
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open FSharp.Control.Tasks
open Giraffe
open Giraffe.Razor

// ---------------------------------
// Models
// ---------------------------------

[<CLIMutable>]
type Person =
{
Name : string
}

[<CLIMutable>]
type CreatePerson =
{
Name : string
CheckMe : bool
}
open Models

// ---------------------------------
// Web app
Expand Down Expand Up @@ -62,6 +45,10 @@ let smallFileUploadHandler =
let largeFileUploadHandler =
fun (next : HttpFunc) (ctx : HttpContext) ->
task {
// Simply accessing this property triggers something necessary
// for the IFormFeature instance to be injected (without this, it's null)
// see https://stackoverflow.com/questions/65967338/how-do-i-enable-iformfeature-in-asp-net-kestrel
let formContentType = ctx.Request.HasFormContentType
let formFeature = ctx.Features.Get<IFormFeature>()
let! form = formFeature.ReadFormAsync CancellationToken.None
return! (form.Files |> displayFileInfos) next ctx
Expand All @@ -81,7 +68,7 @@ let renderPerson =

let renderCreatePerson =
let model = { Name = ""; CheckMe = true }
let viewData = dict [("Title", box "Create peson")]
let viewData = dict [("Title", box "Create person")]
razorHtmlView "CreatePerson" (Some model) (Some viewData) None

let createPerson =
Expand All @@ -97,7 +84,7 @@ let createPerson =
let url = sprintf "/person?name=%s" model.Name
return! redirectTo false url next ctx
else
let viewData = dict [("Title", box "Create peson")]
let viewData = dict [("Title", box "Create person")]
return! razorHtmlView "CreatePerson" (Some model) (Some viewData) (Some modelState) next ctx
}

Expand All @@ -108,11 +95,23 @@ let viewData =
"Bar", true :> obj
]

let renderIndex =
"""
<p>non razor index</p>
<ul>
<li><a href=/razor>razor</a></li></li>
<li><a href=/person>person</a></li></li>
<li><a href=/person/create>person/create</li></a></li>
<li><a href=/upload>upload</a></li>
</ul>
"""
|> htmlString

let webApp =
choose [
GET >=>
choose [
route "/" >=> text "index"
route "/" >=> renderIndex
route "/razor" >=> razorView "text/html" "Hello" None (Some viewData) None
route "/person/create" >=> renderCreatePerson
route "/person" >=> renderPerson
Expand All @@ -132,7 +131,7 @@ let webApp =

let errorHandler (ex : Exception) (logger : ILogger) =
logger.LogError(EventId(), ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> ServerErrors.INTERNAL_ERROR (text ex.Message)
clearResponse >=> ServerErrors.INTERNAL_ERROR ex.Message

// ---------------------------------
// Main
Expand All @@ -148,9 +147,14 @@ let configureServices (services : IServiceCollection) =
let sp = services.BuildServiceProvider()
let env = sp.GetService<IWebHostEnvironment>()
services.AddGiraffe() |> ignore
Path.Combine(env.ContentRootPath, "Views")
services.AddMvc() |> ignore // Required for Razor views to work, regardless of runtime compilation or not.
// Only use Razor runtime compilation in DEBUG (For hot reload to work)
// The statically compiled views are used in Release mode.
#if DEBUG
Path.GetFullPath(Path.Combine(env.ContentRootPath, "..", "RazorClassLibrary"))
|> services.AddRazorEngine
|> ignore
#endif

let configureLogging (loggerBuilder : ILoggingBuilder) =
loggerBuilder.AddFilter(fun lvl -> lvl.Equals LogLevel.Error)
Expand All @@ -168,4 +172,4 @@ let main _ =
.ConfigureLogging(configureLogging)
.Build()
.Run()
0
0
2 changes: 0 additions & 2 deletions samples/GiraffeRazorSample/Views/Partial.cshtml

This file was deleted.

18 changes: 18 additions & 0 deletions samples/Models/Models.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Models

// ---------------------------------
// Models
// ---------------------------------

[<CLIMutable>]
type Person =
{
Name : string
}

[<CLIMutable>]
type CreatePerson =
{
Name : string
CheckMe : bool
}
12 changes: 12 additions & 0 deletions samples/Models/Models.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="Models.fs" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions samples/RazorClassLibrary/RazorClassLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<!--Necessary to support views in the razor library-->
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Models\Models.fsproj" />
</ItemGroup>

<ItemGroup>
<None Include="**\*.cshtml" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@model GiraffeRazorSample.CreatePerson
@model Models.CreatePerson
@{
Layout = "_Layout";
}
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<div>
<h3>Create Person</h3>
<h3>@ViewData["Title"]</h3>
<form method="post" role="form" asp-antiforgery="true">
<div asp-validation-summary="All" class="danger-text"></div>
<p>
Expand Down
3 changes: 3 additions & 0 deletions samples/RazorClassLibrary/Views/Partial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Greetings @ViewData["Title"]</p>
<p>Some partial text.</p>
<a href=/person/create>Create a new person!</a>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model GiraffeRazorSample.Person
@model Models.Person
@{
Layout = "_Layout";
}
Expand Down
7 changes: 3 additions & 4 deletions src/Giraffe.Razor/Giraffe.Razor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<NoWarn>$(NoWarn);NU5104</NoWarn>

<!-- Build settings -->
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<DebugType>portable</DebugType>
<OutputType>Library</OutputType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -45,9 +45,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Giraffe" Version="5.0.0-rc-6" />
<PackageReference Include="Ply" Version="0.3.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.*" />
<PackageReference Include="Giraffe" Version="6.0.0-alpha-2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading