0

I have an Asp.Net Mvc 5 application backend. I want to add in my controller a function that creates html on the fly in a server folder. I want that this html is generated from template. Example:

Template:

<html><head/> <body> {CSharpVar} </body> </html>

in my controller:

MyObject mObj= new MyObject();
mObj.CSharpVar= "String TEst";
MyWantedClass.CreateHtml(htmlTemplate,mObj,outputPath);

What is the best way or best class to use?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
davymartu
  • 1,393
  • 3
  • 15
  • 34
  • Whats wrong with using MVC? – Erik Philips Aug 28 '14 at 14:52
  • 1
    This is a dangerous practice for a public facing website because you have to grant the IIS process write access to the file system. Where are you wanting to write these files? The most straightforward way in code is probably to number the template arguments `{0}, {1}`, etc... Then you could just use `String.Format(htmlTemplateSourceCode, arg1, arg2)` – xDaevax Aug 28 '14 at 14:52
  • 1
    possible duplicate of [How to Render Partial View into a String](http://stackoverflow.com/questions/2537741/how-to-render-partial-view-into-a-string) and can be easily modified for a full view. No need to reinvent the wheel. – Erik Philips Aug 28 '14 at 14:54
  • Depending on what you want to accomplish, a safe templating system like dotliquid might be better. You still need to consider the risks of writing files to your server and ensure they are benign HTML: http://dotliquidmarkup.org/ – AaronLS Aug 28 '14 at 14:55

1 Answers1

0

You could try using MustacheSharp (nuget here), which is a Handlebars template engine, to compile your templates, and use a custom build action in visual studio so they recompile when changed.

David Kirkland
  • 2,431
  • 28
  • 28