Skip to content

Commit 76dcf01

Browse files
committed
net10 support
HtmlSafeTemplate RazorTemplateCache
1 parent cca0f47 commit 76dcf01

10 files changed

Lines changed: 212 additions & 19 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
</PropertyGroup>
77

88
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

ExampleAppCore/HtmlSafeTemplate.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using RazorEngineCore;
2+
3+
namespace ExampleApp
4+
{
5+
public class HtmlSafeTemplate : RazorEngineTemplateBase
6+
{
7+
class RawContent
8+
{
9+
public object Value { get; set; }
10+
11+
public RawContent(object value)
12+
{
13+
Value = value;
14+
}
15+
}
16+
17+
public object Raw(object value)
18+
{
19+
return new RawContent(value);
20+
}
21+
22+
public override void Write(object obj = null)
23+
{
24+
object value = obj is RawContent rawContent
25+
? rawContent.Value
26+
: System.Web.HttpUtility.HtmlEncode(obj);
27+
28+
base.Write(value);
29+
}
30+
31+
public override void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)
32+
{
33+
value = value is RawContent rawContent
34+
? rawContent.Value
35+
: System.Web.HttpUtility.HtmlAttributeEncode(value?.ToString());
36+
37+
base.WriteAttributeValue(prefix, prefixOffset, value, valueOffset, valueLength, isLiteral);
38+
}
39+
}
40+
}

ExampleAppCore/Program.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
1+
using RazorEngineCore;
2+
using System;
23
using System.Collections.Generic;
3-
using RazorEngineCore;
4-
54
namespace ExampleApp
65
{
76
public class TestModel : RazorEngineTemplateBase
@@ -40,13 +39,22 @@ void RecursionTest(int level){
4039
}";
4140

4241
static void Main(string[] args)
42+
{
43+
RegularExample();
44+
CachedHtmlSafeExample();
45+
46+
Console.ReadKey();
47+
}
48+
49+
50+
static void RegularExample()
4351
{
4452
IRazorEngine razorEngine = new RazorEngine();
4553
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
4654

4755
string result = template.Run(new
4856
{
49-
Name = "Alexander",
57+
Name = "<b>Alex</b>",
5058
Items = new List<string>()
5159
{
5260
"item 1",
@@ -55,7 +63,12 @@ static void Main(string[] args)
5563
});
5664

5765
Console.WriteLine(result);
58-
Console.ReadKey();
66+
}
67+
68+
static void CachedHtmlSafeExample()
69+
{
70+
string result = RazorTemplateCache.RenderHtmlSafe("Hello <h1>@Model.Name</h1>", new { Name = "<b>Alex</b>" });
71+
Console.WriteLine(result);
5972
}
6073
}
6174
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using RazorEngineCore;
4+
5+
namespace ExampleApp
6+
{
7+
public static class RazorTemplateCache
8+
{
9+
private static readonly ConcurrentDictionary<string, object> Cache = new(StringComparer.Ordinal);
10+
11+
public static string Render(string template, object? model = null)
12+
{
13+
IRazorEngineCompiledTemplate compiledTemplate = (IRazorEngineCompiledTemplate)Cache.GetOrAdd(template, i =>
14+
{
15+
IRazorEngine razorEngine = new RazorEngine();
16+
return razorEngine.Compile(template);
17+
});
18+
19+
return compiledTemplate.Run(model);
20+
}
21+
22+
public static string RenderHtmlSafe(string template, object? model = null)
23+
{
24+
return Render<HtmlSafeTemplate>(template, model);
25+
}
26+
27+
28+
public static string Render<T>(string template, object? model = null) where T : IRazorEngineTemplate
29+
{
30+
IRazorEngineCompiledTemplate<T> compiledTemplate = (IRazorEngineCompiledTemplate<T>)Cache.GetOrAdd(template, i =>
31+
{
32+
IRazorEngine razorEngine = new RazorEngine();
33+
return razorEngine.Compile<T>(template);
34+
});
35+
36+
if (model.IsAnonymous())
37+
{
38+
model = new AnonymousTypeWrapper(model);
39+
}
40+
41+
return compiledTemplate.Run(instance =>
42+
{
43+
instance.Model = model;
44+
});
45+
}
46+
}
47+
}

ExampleAppNET5/HtmlSafeTemplate.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Threading;
4+
using RazorEngineCore;
5+
6+
namespace ExampleAppNET5
7+
{
8+
public class HtmlSafeTemplate : RazorEngineTemplateBase
9+
{
10+
class RawContent
11+
{
12+
public object Value { get; set; }
13+
14+
public RawContent(object value)
15+
{
16+
Value = value;
17+
}
18+
}
19+
20+
21+
public object Raw(object value)
22+
{
23+
return new RawContent(value);
24+
}
25+
26+
public override void Write(object obj = null)
27+
{
28+
object value = obj is RawContent rawContent
29+
? rawContent.Value
30+
: System.Web.HttpUtility.HtmlEncode(obj);
31+
32+
base.Write(value);
33+
}
34+
35+
public override void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)
36+
{
37+
value = value is RawContent rawContent
38+
? rawContent.Value
39+
: System.Web.HttpUtility.HtmlAttributeEncode(value?.ToString());
40+
41+
base.WriteAttributeValue(prefix, prefixOffset, value, valueOffset, valueLength, isLiteral);
42+
}
43+
}
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Threading;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using RazorEngineCore;
6+
7+
namespace ExampleAppNET5
8+
{
9+
public static class RazorTemplateCache
10+
{
11+
private static readonly ConcurrentDictionary<string, object> Cache = new(StringComparer.Ordinal);
12+
13+
public static string Render(string template, object? model = null)
14+
{
15+
IRazorEngineCompiledTemplate compiledTemplate = (IRazorEngineCompiledTemplate)Cache.GetOrAdd(template, i =>
16+
{
17+
IRazorEngine razorEngine = new RazorEngine();
18+
return razorEngine.Compile(template);
19+
});
20+
21+
return compiledTemplate.Run(model);
22+
}
23+
24+
public static string RenderHtmlSafe(string template, object? model = null)
25+
{
26+
return Render<HtmlSafeTemplate>(template, model);
27+
}
28+
29+
30+
public static string Render<T>(string template, object? model = null) where T : IRazorEngineTemplate
31+
{
32+
IRazorEngineCompiledTemplate<T> compiledTemplate = (IRazorEngineCompiledTemplate<T>)Cache.GetOrAdd(template, i =>
33+
{
34+
IRazorEngine razorEngine = new RazorEngine();
35+
return razorEngine.Compile<T>(template);
36+
});
37+
38+
if (model.IsAnonymous())
39+
{
40+
model = new AnonymousTypeWrapper(model);
41+
}
42+
43+
return compiledTemplate.Run(instance =>
44+
{
45+
instance.Model = model;
46+
});
47+
}
48+
}
49+
}

Pack.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dotnet build -c Release
22
dotnet test
33
dotnet pack -c Release -o artifacts RazorEngineCore\RazorEngineCore.csproj -p:symbolPackageFormat=snupkg --include-symbols
4-
dotnet nuget push artifacts\RazorEngineCore.2024.4.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY
4+
dotnet nuget push artifacts\RazorEngineCore.2026.4.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY

RazorEngineCore.Tests/RazorEngineCore.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net472;net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

RazorEngineCore.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 17
4-
VisualStudioVersion = 17.3.32721.290
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.3.11312.210 d18.3
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorEngineCore", "RazorEngineCore\RazorEngineCore.csproj", "{1E21186E-49A5-433A-ABAD-18CED1AA2494}"
77
EndProject
@@ -12,7 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
Pack.ps1 = Pack.ps1
1313
EndProjectSection
1414
EndProject
15-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleAppCore", "ExampleAppCore\ExampleAppCore.csproj", "{38338B92-1D09-41FB-AE11-3783DA7AFE0B}"
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleAppNET10", "ExampleAppCore\ExampleAppNET10.csproj", "{38338B92-1D09-41FB-AE11-3783DA7AFE0B}"
1616
EndProject
1717
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleAppNET472", "ExampleAppNet472\ExampleAppNET472.csproj", "{D27C1578-BFF9-4469-9099-DAF64450BBBE}"
1818
EndProject

RazorEngineCore/RazorEngineCore.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.0</TargetFrameworks>
3+
<TargetFrameworks>net10.0;net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.0</TargetFrameworks>
44
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5-
<Version>2024.4.1</Version>
5+
<Version>2026.1.1</Version>
66
<Authors>Alexander Selishchev, Simon Mourier, William David Cossey, Benjamin Smith, Dag H. Baardsen, krmr, jddj007-hydra, shehrozeee, TheAtomicOption, ItWorksOnMyMachine</Authors>
77
<PackageProjectUrl>https://github.com/adoconnection/RazorEngineCore</PackageProjectUrl>
8-
<Description>NET6 Razor Template Engine</Description>
9-
<AssemblyVersion>2024.4.1</AssemblyVersion>
10-
<FileVersion>2024.4.1</FileVersion>
8+
<Description>NET10 Razor Template Engine</Description>
9+
<AssemblyVersion>2026.1.1</AssemblyVersion>
10+
<FileVersion>2026.1.1</FileVersion>
1111
<PackageReleaseNotes></PackageReleaseNotes>
1212
<Company>Alexander Selishchev</Company>
1313
<SignAssembly>true</SignAssembly>
@@ -21,9 +21,9 @@
2121
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2222
</PropertyGroup>
2323
<ItemGroup>
24-
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.25" />
25-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
26-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
24+
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.36" />
25+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
26+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
2727
</ItemGroup>
2828
<PropertyGroup>
2929
<PackageLicenseExpression>MIT</PackageLicenseExpression>

0 commit comments

Comments
 (0)