I thought it would be interesting if I could use the new MVC Razor View engine as a mail merge technology. It can still be part of an MVC website and does not have to be stand-alone console app.
Example:
string myTemplate = "Hello @Name, How are you today?";
ViewModel.Name = "Billy Boy";
string output = RazorViewEngineRender( myTemplate, ViewModel );
Then the string output = "Hello Billy Boy, How are you today?"
The main thing is I want the template to be driven from a string rather than a view or partialview.
Does anyone know if this is possible ?
UPDATE:
Ben and Matt made a project on codeplex: http://razorengine.codeplex.com/
Warning
This is some ugly ugly code that was hacked together without testing it other than getting it to work properly.
VirtualPathProvider
Since we're not dealing with actual views on the server we have to add our own path provider to tell MVC where to get our dynamically generated templates. There should be more tests like checking the strings Dictionary to see if the view has been added.
Render Class
This class takes your template as a constructor parameter and adds it to a static Dictionary that is then read by the
VirtualPathProvider
above. You then callRender
and you can optionally pass in a model. This will add the fully qualified model type to the@inherits
and prepend that to the file contents.Global.asax
In your global.asax file you'll have to add the following to the
Application_Start
Calling the code
Notes
This only works with typed Models. I attempted to pass in a new { Name = "Billy Boy" } and it's throwing errors. I'm not sure why and didn't really look too deeply into it.
This was fun, thanks for asking this question.
Razor was designed with standalone operation in mind. There isn't much documentation about that mode yet (since it's all still under development) but have a look at this blog post by Andrew Nurse: http://vibrantcode.com/blog/2010/7/22/using-the-razor-parser-outside-of-aspnet.html