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

7

22

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.

Tremayne answered 25/6, 2012 at 11:7 Comment(2)
I agree with you, storing static html is the most efficient way and most of the time much better then using output caching or other caching mechanism.Ballesteros
I recently heard about DocPad which does basically what you are asking about except it's not ASP.Net or Razor. I wish there was an ASP.Net/MVC3/Razor product like DocPad. I think there's a place for using a dynamic rendering engine to create a static site. I work with multiple sites that would benefit from this idea. When you make an update you regen the site and either upload the entire site again or use a diff tool to filter out the files that changed and upload those.Kass
T
1

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.

Tremayne answered 17/9, 2012 at 7:16 Comment(0)
P
7
  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.
Pickaxe answered 25/6, 2012 at 11:19 Comment(3)
Your point 2. is interessting. is there a way to have controller and render these razor view from a console app?Tremayne
@Tremayne Did you got it working? I'm also interested to address the similar issue I'm facing where in I'm also looking to store static html pages generated for a site built on asp.net mvc3. Update here if you found a solution... thanksBallesteros
Do you think whether is this useful.. awesome.codeplex.com/…Ballesteros
O
4

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);
        }
    }
}
Osteomyelitis answered 25/6, 2012 at 12:16 Comment(6)
Is there a way to use template files(cshtml) with this solutions?Tremayne
It seems really near what I was thinking to. Let me some days to test it :)Tremayne
Just one question, with this, does templates works? And partial view? Because I will have more than 60% of my views that will never changeTremayne
Good question, I haven't tried partials and I don't think that will work since the engine is string based, not file based like razor in MVCOsteomyelitis
I saw on the authors blog that it's planned(and partial view too) for the V3Tremayne
Argh, I've to handle Localization in the template, and I don't think it will be possible with razor engine :(Tremayne
P
2

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

Pryce answered 25/6, 2012 at 11:13 Comment(2)
I'm well aware of caching, but it's not what I'm asking. I just can't. Our web pages will be integrated in a much bigger website(running on apache).Tremayne
@Tremayne As caching is most likely the obvious solution to your question, I would edit your question to specify that you don't want to use caching to avoid more caching related answers.Pryce
T
1

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.

Tremayne answered 17/9, 2012 at 7:16 Comment(0)
S
1

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.

Shavonneshaw answered 9/5, 2015 at 23:6 Comment(1)
Could you please details and if possible a github repo. Currently need to do this.Ashlieashlin
W
0

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

Wentzel answered 25/6, 2012 at 11:14 Comment(1)
~2500 web pages, it's an official website which give access to laws elements in 5 languages. Caching is not what I need.Tremayne
J
0

Look at T4 templates or a similar templating solution

Jannelle answered 25/6, 2012 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.