Use razor/asp.net mvc3 to generate static html pag

2019-02-03 02:40发布

问题:

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.

回答1:

  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.


回答2:

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);
        }
    }
}


回答3:

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



回答4:

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



回答5:

Look at T4 templates or a similar templating solution



回答6:

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.



回答7:

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.