Asp html template engine
NET Core Library instead of. NET Standard Library. NET Core insurance application at work. HTML emails are the absolute worst. Inlined styles. Nested tables. Different apps and platforms render the markup differently.
So fine, HTML emails. NET Class Library, so that the sending of the emails happens right next to all my other business logic. That way I can re-purpose this logic into an ASP. NET Core app or a.
Razor checks the box for 1, because it already has the concept of a Layout view and a child view. It also is a good fit for 2, because it lets you re-use UI components via Partials among other methods. In fact, you can actually achieve 1, 2, and 3 in regular ASP.
NET 4. NET or pre NET Core pre NET Core applications. The simplest way to think about Razor Class Libraries is if you add a View in your Razor Class Library, it essentially gets copied down into that same relative path into your main application. So if you add an Index. NET Core Application. But the question is would it find those files in a normal. The answer is — yes. NET Core application. Again — all code can be found here on GitHub.
First — create an ASP. Add this class to your Razor Class Library you just created. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters using Microsoft. Http ; using Microsoft. Mvc ; using Microsoft.
Abstractions ; using Microsoft. ModelBinding ; using Microsoft. Razor ; using Microsoft. Rendering ; using Microsoft. ViewEngines ; using Microsoft. ViewFeatures ; using Microsoft. Routing ; using System ; using System. IO ; using System. Linq ; using System. Tasks ; namespace RazorHtmlEmails. RenderAsync viewContext ; return output. Concat findViewResult. Join Environment. However, all I really care about for the layout is everything outside of the white box for my layout.
Get ready to dive into your new account. ViewData allows us to pass messages from a child view up to the parent view.
In a real application, I would have pulled that Welcome text down to the child view as well, but I left that as a demonstration of the ability for the child view to dynamically change the parent EmailLayout. Some more examples of what you could do with ViewData could be the child view wants to dictate which Logo to use or what color the background is, or literally anything you want.
The next thing I want to do is start to make reusable components via partial views in order to abstract away the complexity of certain HTML Email components, as well as always providing a consistent look to the end user. All that really needs to be dynamic about this is the text and the link.
That should be easy enough. Learn more about bidirectional Unicode characters Show hidden characters await Html. That saves us from copying and pasting that crazy table code around every time we need a button for our Emails, which is useful.
The last thing we need to do in our Razor Class Library is add our custom email. This folder will house our ConfirmAccount email logic. ConfirmAccount using RazorHtmlEmails. First, you need to confirm your account. Just press the button below. Right now is where it all starts to come together, and you can see the power of being able to use Razor to build out our HTML emails. On our day-to-day emails that we build out for the rest of our application, we no longer have to worry about gross table syntax or inline style craziness, we can just focus on the custom content that makes up that HTML Email.
But one of the coolest things about this, is the fact that we can call this code from regular. If you want you can create a.
Learn more about bidirectional Unicode characters Show hidden characters using MailKit. Smtp ; using MimeKit ; using MimeKit. Text ; using RazorHtmlEmails. Services ; using RazorHtmlEmails. ConfirmAccount ; using System ; using System. Generic ; using System. Connect " Send message ; client.
As an aside, for testing local emails without an email server, I love using Papercut. If some paid service that does the same thing as Papercut does and wants to sponsor this blog, feel free to reach out to me and get rejected because you will never get me to give up Papercut.
The last step we have to do is to hook this up to the UI. Learn more about bidirectional Unicode characters Show hidden characters builder. If using the Startup. RazorPages ; using RazorHtmlEmails. Common ; namespace RazorHtmlEmails. Register " testmctestyface contoso.
Page ". When I run the app, and open up Papercut, I get my email in all its glory. This is great article, very well crafted and detailed, with a dash of humour. Thank you for taking the time to simplify this task. God bless. This is a great article, very well crafted and detailed, with a dash of humour. Hi Joseph, do you have any code you could share by chance that reproduces the issue?
Happy to help, but not sure where to start. RenderViewToStringAsync a few times in a row will be fine. I want to create that feature of Templates from Joomla in Asp. Net MVC. On a side-note, since this CMS will be used in-house, do I even need to create the template functionality? We will create multiple websites using this CMS. For now the administration section of the website is provided by the CMS. Then based on the template we buy from TemplateMonster we write the front-end of the application.
I hope I am being clear and apologize if I'm not. You can check out the repository here. Please do leave a star if you find it useful! MailingNinja - A Boilerplate Repository. Enjoy this article? Buy Me a Coffee. Join the community! Like on Facebook Follow on Twitter. I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. Create a String writer stream and have a ViewContext be created basing on the actioncontext, viewdata and the stringwriter stream render the view over the context and have the output be written on the stringwriter return the string written and close the stream.
Setting up the ActionContext: We make use of the IServiceProvider instance from the aspnetcore container by having it injected through the constructor. FindView actionContext, viewName, false ; if viewResult. Setting up the ViewDataDictionary We pass the model passed to the method as an argument to the ViewDataDictonary class, which passes the model data to the RazorView for rendering.
Imagination is our only limiting factor when using the Razor engine. The first step is to install the. NET package. This package uses. NET Standard 2, so it should work for.
The library caches all template compilations using the template key parameter. Our code can reuse the template using the Run command. Let us modify our application and see how that happens. Note that the first call is to RunCompile , and the second call is to Run. The resulting output is unsurprising.
0コメント