Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use razor/asp.net mvc3 to generate static html pages?

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# code, which takes data from a SQL Server database.

Data will be not often changed(every 4-5 month), and this website will be highly frequented.

Since I find the razor synthax of asp.net MVC3 very effective, I was wondering if it's possible to use asp.net MVC3/Razor to generate those .html pages?

So:

  1. Is this a good idea?
  2. If yes, what is the good way?
  3. If you think to another good manner of doing it, which way?

Thank you for the help

Edit

Regarding answers, I need to make a precision: I don't want/need to use web caching, for a lot of reasons(load(millions of pages loaded every month), integration(we integrate our page in an optimized apache with, another part of a website), number of pages(caching will only help me if I've the same pages a lot of time, but I will have ~2500 pages, so with murphy's law, except if I put a very high cache timeout, I will have to generate them often). So I really search something to generate HTML pages.

Edit 2 I just got a new constraint :/ Those template must be localized. Meaning that I should have something equivalent to the following razor code: @MyLocalizationFile.My.MyValue

Edit 3 Currently, I'm thinking of doing a dynamic website, and call some http query on it, to store the generated HTML. BUT, is there a way to avoid the http? meaning simulate an http call, specifiy the output stream and the url called(with only GET call).

Our previous load numbers were really underestimated, actually they have a little more than one million visitor each days, ~ 14 million pages loads/day.

like image 639
J4N Avatar asked Jun 25 '12 11:06

J4N


4 Answers

  1. Yes it is. Even when you can cache the results, HTML pages will be always faster and use lower server resources
  2. A good way is to transform your razor views into text and then save the text as a html file.
  3. Another way can be using T4 templates, but I recommend Razor.
like image 68
Eduardo Molteni Avatar answered Nov 18 '22 14:11

Eduardo Molteni


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);
        }
    }
}
like image 42
John Landheer Avatar answered Nov 18 '22 15:11

John Landheer


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.

like image 1
J4N Avatar answered Nov 18 '22 15:11

J4N


Rather than generating static HTML pages, I think it would be better to dynamically generate the pages each time, but using Caching to increase performance.

See this article on caching with ASP.NET MVC3 for more information:

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs

like image 2
Curtis Avatar answered Nov 18 '22 16:11

Curtis