11//*******************************
22// Thx https://github.com/aspnet/Entropy/tree/master/samples/Mvc.RenderViewToString
33//*******************************
4+
5+ using System ;
46using Microsoft . AspNetCore . Hosting ;
57using Microsoft . AspNetCore . Hosting . Internal ;
68using Microsoft . AspNetCore . Mvc . Razor ;
1113using SmartCode . Utilities ;
1214using System . Collections . Generic ;
1315using System . Diagnostics ;
16+ using System . IO ;
1417using System . Reflection ;
1518using System . Text . Encodings . Web ;
1619using System . Threading . Tasks ;
20+ using Microsoft . Extensions . FileProviders . Physical ;
1721
1822namespace 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