Brain dead way to call Razor from a non-Web application

160 views
Skip to first unread message

Marcelo Calbucci

unread,
May 22, 2012, 8:44:20 PM5/22/12
to dotnet...@googlegroups.com
Hey folks, I'm trying to do something pretty "easy".

I want to use Razor to render email templates for our users, like Welcome email, reset your password, etc. Some of these emails are coming from offline services on our system, so they are not running on the Web service. Currently we have a hack that makes a call to the Web Service which in turn renders the emails using the Razor view engine and send it to the user. I'd like to be able to send that email from wherever.

RazorEngine which is a free component on NuGet it's pretty awesome converting any Razor-based syntax text into a final text, but it doesn't support Layouts or nested partial views.

MvcMailer seems to have been abandoned and I can't get the package to work.

The requirements I have are pretty simple:
1) I want to have a common layout for all the emails
2) The email body should be a CSHTML with some Model information
3) I want to be able to re-use elements (by calling Partial-views).
4) It has to run into "console" mode (i.e., no Web Service attached)

Has anyone done this?

--
-Marcelo

@calbucci | blog.calbucci.com 

Jeff MacDuff

unread,
May 22, 2012, 10:25:42 PM5/22/12
to dotnet...@googlegroups.com

For what it’s worth we had a similar requirement to be able to send “rich” emails from both our webservice, and offline services in our infrastructure.  We decided to leverage Mailchimp templates sent through our normal mail server. We have an internal email service (which our other services consume) that grabs the mailchimp template requested , puts in the info for the email , and sends the email through our normal email server.

 

The major advantage is that you can get nice mailchimp templates created through elance/odesk/etc basically overnight for ~$40. This allows us to have good looking email templates whenever we want while leveraging a commoditized layout system (mailchimp).

 

We don’t actually send any email through mailchimp right now, most of our emails are one offs like “welcome”.

 

 

Jeff MacDuff

CTO & Co-Founder, Buddy

Email:   je...@buddy.com

Tom Lianza

unread,
May 23, 2012, 1:20:45 AM5/23/12
to dotnet...@googlegroups.com
We use the same hack you're describing, but we then lock that path in IIS so it's only accessible to certain IP addresses.  That way it can be called from "anywhere" so long as anywhere is (a) on the internet, (b) at a pre-known location.  

I don't know of an MVC equivalent of Rails' ActionMailer unfortunately, but am also interested in any solutions others have found.

Tom

Stephen Curial

unread,
May 23, 2012, 1:51:41 PM5/23/12
to dotnet...@googlegroups.com
We've been using MvcMailer 1.1 and ASP.Net MVC 3 for a while now without any issues and it's worked great. What version of MVC are you using? What errors are you getting?

-- 
Stephen Curial, Co-Founder

 Pure Project Tools | Online Project Management Software | www.PureProjectTools.com

Marcelo Calbucci

unread,
May 23, 2012, 1:54:50 PM5/23/12
to dotnet...@googlegroups.com
Hey Stephen, where did you get the MvcMailer from? The problem was with the Scaffolding tool that I couldn't get to work and the documentation was very light.

Stephen Curial

unread,
May 23, 2012, 2:25:05 PM5/23/12
to dotnet...@googlegroups.com
We used nuget for this package. I followed this tutorial: 

What kind of error messages are you getting? I've seen some issues complaining about a croupt file. If that's the case you can try replacing your scaffolding.config file. Here's ours:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DefaultScaffolders>
    <Default DefaultName="CustomScaffolder" ScaffolderName="T4Scaffolding.CustomScaffolder" />
    <Default DefaultName="CustomTemplate" ScaffolderName="T4Scaffolding.CustomTemplate" />
    <Default DefaultName="DbContext" ScaffolderName="T4Scaffolding.EFDbContext" />
    <Default DefaultName="Mailer" ScaffolderName="Mailer.Razor" />
    <Default DefaultName="Repository" ScaffolderName="T4Scaffolding.EFRepository" />
  </DefaultScaffolders>
</Config>

Marcelo Calbucci

unread,
May 23, 2012, 2:50:32 PM5/23/12
to dotnet...@googlegroups.com
This is what I get:

Cannot resolve alias 'Scaffold' because it refers to term 'Invoke-Scaffolder', which is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:9
+ Scaffold <<<<  Mailer.Razor UserMailer Welcome,PasswordReset
    + CategoryInfo          : ObjectNotFound: (Scaffold:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : AliasNotResolvedException

Marcelo Calbucci

unread,
May 23, 2012, 5:18:24 PM5/23/12
to dotnet...@googlegroups.com
I figured the problem was having an outdated version of MvcScaffolding. I had to uninstall, re-install, restart Visual Studio and then start the MvcMailer package again.

Thanks.
Reply all
Reply to author
Forward
0 new messages