For one projet, I\'ve to generate static .html pages, which are gonna to be published on a remote server.
I\'ve to automate the creation of those files from a c# cod
Is there any performance reason you've run into that would merit the effort of pre-rendering the website? How many pages are we talking about? What kind of parameters do your controllers take? If vanilla caching does not satisfy your requirements, for me the best approach would be a disk-based caching provider...
http://www.juliencorioland.net/Archives/en-aspnet-mvc-custom-output-cache-provider
You can use the Razor Engine (NuGet-link, their website), This way you can create templates from a console application without using asp.net MVC.
I use it as follows:
public string ParseFile<T>(string fileName, T model) {
var file = File.OpenText(fileName);
var sb = new StringBuilder();
string line;
while ((line = file.ReadLine()) != null)
{
// RazorEngine does not recognize the @model line, remove it
if (!line.StartsWith("@model ", StringComparison.OrdinalIgnoreCase))
sb.AppendLine(line);
}
file.Close();
// Stuff to make sure we get unescaped-Html back:
var config = new FluentTemplateServiceConfiguration(
c => c.WithEncoding(RazorEngine.Encoding.Raw));
string result;
using (var service = new TemplateService(config))
{
return service.Parse<T>(sb.ToString(), model);
}
}
}
I ended by creating a normal asp.net MVC website and then generate page by going on the page with a WebClient.
Like this I can have a preview of the website and I can enjoy the full power of Razor+MVC helpers.
I'm working on a similar solution. My website is running normally (ASP.NET + DB + CMS) on a staging environment and then I use wget to crawl it and generate static html pages. Those static html pages, including assets are then uploaded to a Amazon S3 Bucket. That way the website becomes fully static, with no dependencies.
I'm planning to have a daily task that crawls specific pages on the website to make it speedier, e.g. only crawl /news every day.
I know you've already found a solution, but maybe this answer might be helpful to others.
Look at T4 templates or a similar templating solution