Skip to content

EarthML/EarthML.Templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Razor Templates example

Ran into a few issues, starting out with added @page in the razor pages. Doing so result in null exceptions

Unhandled Exception: System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at AspNetCore.Templates_WelcomeMail.get_Model()
   at AspNetCore.Templates_WelcomeMail.<ExecuteAsync>d__0.MoveNext() in C:\dev\EarthML\EarthML.Templates\src\EarthML.Templates\Templates\WelcomeMail.cshtml:line 7
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderPageCoreAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderPageAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Mvc.RenderViewToString.RazorViewToStringRenderer.<RenderViewToStringAsync>d__4`1.MoveNext() in C:\dev\EarthML\EarthML.Templates\src\EarthML.Templates.Cli\RazorViewToStringRenderer.cs:line 58
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at Mvc.RenderViewToString.Program.Main() in C:\dev\EarthML\EarthML.Templates\src\EarthML.Templates.Cli\Program.cs:line 33

but looking at entropy example i found that @page should not be used.

Also, when moving things to a templates folder instead of views, the partial and layouts needs to be in 'templates/shared' folder instead of the root views folder. I have not found a way to config this.

But everything works now and running will give the template rendered 3 times, first for the default, second for a template in a shared library and last using a seperate rendering abstracting also in a seperate class library.

        public static void Main()
        {
            var serviceScopeFactory = InitializeServices();
            {
                var emailContent = RenderViewAsync(serviceScopeFactory, "Templates/EmailTemplate.cshtml").Result;

                Console.WriteLine(emailContent);

            }

            
            {
                var emailContent = RenderViewAsync(serviceScopeFactory, "Templates/WelcomeMail.cshtml").Result;

                Console.WriteLine(emailContent);

            }

            {

               
                var a = new EmailTemplateService();
                Console.WriteLine(a.RenderViewAsync().Result);
            }
        }

output:

<!DOCTYPE html>

<html>
<body>
    <div>

Hello User,

<p>
    This is a generic email about something.<br />
    It also contains some content that is generated by a partial view.<br />

    This is the partial view content.<br />
    A user specific report based on values 1 and 2.<br />
    <br />
</p>
    </div>
    <footer>
Thanks,<br />
Sender
    </footer>
</body>
</html>

<!DOCTYPE html>

<html>
<body>
    <div>


Hello User,

<p>
    This is a generic email about something.<br />
    It also contains some content that is generated by a partial view.<br />

    This is the partial view content.<br />
    A user specific report based on values 1 and 2.<br />
    <br />
</p>
    </div>
    <footer>
        Thanks,<br />
        Sender
    </footer>
</body>
</html>
<!DOCTYPE html>

<html>
<body>
    <div>


Hello User,

<p>
    This is a generic email about something.<br />
    It also contains some content that is generated by a partial view.<br />

    This is the partial view content.<br />
    A user specific report based on values 1 and 2.<br />
    <br />
</p>
    </div>
    <footer>
        Thanks,<br />
        Sender
    </footer>
</body>
</html>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages