Skip to content

Commit 1897c81

Browse files
committed
add support Absolute Path Template
1 parent 4a322a2 commit 1897c81

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>2</VersionMajor>
44
<VersionMinor>2</VersionMinor>
5-
<VersionPatch>40</VersionPatch>
5+
<VersionPatch>42</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>

src/SmartCode.TemplateEngine/Impl/OfficialRazorTemplateEngine.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//*******************************
22
// Thx https://github.com/aspnet/Entropy/tree/master/samples/Mvc.RenderViewToString
33
//*******************************
4+
5+
using System;
46
using Microsoft.AspNetCore.Hosting;
57
using Microsoft.AspNetCore.Hosting.Internal;
68
using Microsoft.AspNetCore.Mvc.Razor;
@@ -11,17 +13,21 @@
1113
using SmartCode.Utilities;
1214
using System.Collections.Generic;
1315
using System.Diagnostics;
16+
using System.IO;
1417
using System.Reflection;
1518
using System.Text.Encodings.Web;
1619
using System.Threading.Tasks;
20+
using Microsoft.Extensions.FileProviders.Physical;
1721

1822
namespace SmartCode.TemplateEngine.Impl
1923
{
2024
public class OfficialRazorTemplateEngine : ITemplateEngine
2125
{
26+
private const string TEMP = ".temp";
2227
public bool Initialized { get; private set; }
2328
public string Name { get; private set; } = "Razor";
2429
private string _root = AppPath.Relative("RazorTemplates");
30+
private string _temp;
2531
private IServiceScopeFactory _scopeFactory;
2632
public void Initialize(IDictionary<string, object> parameters)
2733
{
@@ -37,15 +43,34 @@ public void Initialize(IDictionary<string, object> parameters)
3743
_root = root;
3844
}
3945
}
46+
_temp = Path.Combine(_root, TEMP);
47+
if (!Directory.Exists(_temp))
48+
{
49+
Directory.CreateDirectory(_temp);
50+
}
4051
InitializeServices();
4152
}
4253

43-
public Task<string> Render(BuildContext context)
54+
public async Task<string> Render(BuildContext context)
4455
{
4556
using (var serviceScope = _scopeFactory.CreateScope())
4657
{
4758
var helper = serviceScope.ServiceProvider.GetRequiredService<OfficialRazorViewToStringRenderer>();
48-
return helper.RenderViewToStringAsync(context.Build.TemplateEngine.FullPath, context);
59+
var viewPath = context.Build.TemplateEngine.FullPath;
60+
if (Path.IsPathRooted(viewPath))
61+
{
62+
var tempFileName = $"{Path.GetFileNameWithoutExtension(viewPath)}-{Guid.NewGuid():N}{Path.GetExtension(viewPath)}";
63+
var destFileName = Path.Combine(_temp, tempFileName);
64+
File.Copy(context.Build.TemplateEngine.FullPath, destFileName);
65+
viewPath = Path.Combine(TEMP, tempFileName);
66+
var result = await helper.RenderViewToStringAsync(viewPath, context);
67+
File.Delete(destFileName);
68+
return result;
69+
}
70+
else
71+
{
72+
return await helper.RenderViewToStringAsync(viewPath, context);
73+
}
4974
}
5075
}
5176

@@ -58,7 +83,7 @@ private void InitializeServices()
5883
private IServiceCollection ConfigureDefaultServices()
5984
{
6085
var services = new ServiceCollection();
61-
IFileProvider fileProvider = new PhysicalFileProvider(_root);
86+
IFileProvider fileProvider = new PhysicalFileProvider(_root, ExclusionFilters.None);
6287
services.AddSingleton<IHostingEnvironment>(new HostingEnvironment
6388
{
6489
ApplicationName = Assembly.GetEntryAssembly().GetName().Name,

0 commit comments

Comments
 (0)