Skip to content

Commit bddfeca

Browse files
committed
Add F# version of HtmlTemplateBase and re-org solution.
1 parent e2d9f34 commit bddfeca

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

WebApiContrib.Formatting.Razor.sln

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.Formatting.Razor", "src\WebApiContrib.Formatting.Razor\WebApiContrib.Formatting.Razor.csproj", "{8C15B0D3-9108-4EA3-AE19-3C9571266C47}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcWebApiSiteTest", "samples\MvcWebApiSiteTest\MvcWebApiSiteTest.csproj", "{5D90E626-1C40-4B23-887A-DC2527D696CB}"
@@ -9,6 +11,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.Razor.Tests",
911
EndProject
1012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.Formatting.Razor.Templating", "src\WebApiContrib.Formatting.Razor.Templating\WebApiContrib.Formatting.Razor.Templating.csproj", "{36287DD3-6930-4C13-93B7-06FE600278F9}"
1113
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fsharp", "fsharp", "{1258C816-9FA3-43F5-85C7-AA2BE6E842DE}"
15+
ProjectSection(SolutionItems) = preProject
16+
fsharp\HtmlTemplateBase.fs = fsharp\HtmlTemplateBase.fs
17+
fsharp\WebApiContrib.Formatting.Razor.fs = fsharp\WebApiContrib.Formatting.Razor.fs
18+
EndProjectSection
19+
EndProject
1220
Global
1321
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1422
Debug|Any CPU = Debug|Any CPU

fsharp/HtmlTemplateBase.fs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace WebApiContrib.Formatting.Razor
2+
3+
#if INTERACTIVE
4+
#I "../packages"
5+
#r "System.Net.Http"
6+
#r "Microsoft.AspNet.Mvc.5.0.0/lib/net45/System.Web.Mvc.dll"
7+
#r "Microsoft.AspNet.Razor.3.0.0/lib/net45/System.Web.Razor.dll"
8+
#r "Microsoft.AspNet.WebApi.Client.5.0.0/lib/net45/System.Net.Http.Formatting.dll"
9+
#r "Microsoft.AspNet.WebApi.Core.5.0.0/lib/net45/System.Web.Http.dll"
10+
#r "RazorEngine.3.3.0/lib/net40/RazorEngine.dll"
11+
#r "WebApiContrib.Formatting.Html.2.0.0/lib/net45/WebApiContrib.Formatting.Html.dll"
12+
#endif
13+
14+
open System
15+
open System.Collections.Generic
16+
open System.IO
17+
open System.Web
18+
open System.Web.Mvc
19+
open System.Web.Routing
20+
open RazorEngine.Configuration
21+
open RazorEngine.Templating
22+
open RazorEngine.Text
23+
24+
/// <summary>
25+
/// A <see cref="TemplateBase"/> class for use when rendering HTML with Razor in Web API applications.
26+
/// </summary>
27+
/// <typeparam name="T">The Model type.</typeparam>
28+
[<AbstractClass>]
29+
[<RequireNamespaces("System.Web.Mvc.Html")>]
30+
type HtmlTemplateBase<'T>() =
31+
inherit TemplateBase<'T>()
32+
33+
let urlHelper = lazy (new System.Web.Http.Routing.UrlHelper())
34+
let mutable viewData : ViewDataDictionary = null
35+
36+
/// <summary>
37+
/// Writes markup to the specified <paramref name="writer"/>, encoding by default
38+
/// except in cases where the <paramref name="value"/> is already encoded.
39+
/// </summary>
40+
/// <param name="writer">The current <see cref="TextWriter"/>.</param>
41+
/// <param name="value">The value to write to the <paramref name="writer"/>.</param>
42+
/// <see href="http://stackoverflow.com/questions/19431365/razorengine-html-helpers-work-but-escape-the-html"/>
43+
override this.WriteTo(writer: TextWriter, value: obj) =
44+
if writer = null then
45+
raise (new ArgumentNullException("writer"))
46+
47+
if value = null then () else
48+
let valueType = value.GetType()
49+
if typeof<IEncodedString>.IsAssignableFrom(valueType) then
50+
let encodedString = unbox<IEncodedString> value
51+
writer.Write(encodedString)
52+
elif typeof<IHtmlString>.IsAssignableFrom(valueType) then
53+
let htmlString = unbox<IHtmlString> value
54+
writer.Write(htmlString.ToHtmlString())
55+
else
56+
// This was the base template's implementation:
57+
writer.Write(this.TemplateService.EncodedStringFactory.CreateEncodedString(value))
58+
59+
/// <summary>
60+
/// Returns a <see cref="System.Web.Mvc.HtmlHelper{T}"/> based on the current Model type.
61+
/// </summary>
62+
member this.Html =
63+
// NOTE: Removed caching, as that would block updates the ViewData property, which has a setter.
64+
let writer = this.CurrentWriter
65+
let viewContext = new ViewContext(Writer = writer, ViewData = this.ViewData)
66+
new HtmlHelper<'T>(viewContext, this)
67+
68+
/// <summary>
69+
/// Returns a <see cref="System.Web.Http.Routing.UrlHelper"/>.
70+
/// </summary>
71+
/// <remarks>
72+
/// This is not currently tied to the current <see cref="System.Net.Http.HttpRequestMessage"/> but should be.
73+
/// </remarks>
74+
member this.Url = urlHelper.Force()
75+
76+
/// <summary>
77+
/// Returns a <see cref="ViewDataDictionary"/> typed to the Model's type.
78+
/// </summary>
79+
member this.ViewData
80+
with get() : ViewDataDictionary =
81+
if viewData = null then
82+
viewData <- new ViewDataDictionary<'T>(TemplateInfo = new TemplateInfo(HtmlFieldPrefix = ""), Model = this.Model)
83+
viewData
84+
and set(value) =
85+
viewData <- value
86+
87+
interface IViewDataContainer with
88+
member this.ViewData
89+
with get() = this.ViewData
90+
and set(value) = this.ViewData <- value

src/WebApiContrib.Formatting.Razor/WebApiContrib.Formatting.Razor.fs renamed to fsharp/WebApiContrib.Formatting.Razor.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace WebApiContrib.Formatting.Razor
22

33
#if INTERACTIVE
4-
#I "../../packages"
4+
#I "../packages"
55
#r "System.Net.Http"
66
#r "Microsoft.AspNet.WebApi.Client.5.0.0/lib/net45/System.Net.Http.Formatting.dll"
77
#r "Microsoft.AspNet.WebApi.Core.5.0.0/lib/net45/System.Web.Http.dll"

src/WebApiContrib.Formatting.Razor/WebApiContrib.Formatting.Razor.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@
6969
<ItemGroup>
7070
<None Include="packages.config" />
7171
</ItemGroup>
72-
<ItemGroup>
73-
<None Include="WebApiContrib.Formatting.Razor.fs" />
74-
</ItemGroup>
7572
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7673
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
7774
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

0 commit comments

Comments
 (0)