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

后端 未结 7 1622
一整个雨季
一整个雨季 2020-12-29 05:55

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

相关标签:
7条回答
  • 2020-12-29 06:12

    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

    0 讨论(0)
  • 2020-12-29 06:14

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 06:20
    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.
    0 讨论(0)
  • 2020-12-29 06:20

    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.

    0 讨论(0)
  • 2020-12-29 06:20

    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.

    0 讨论(0)
  • 2020-12-29 06:21

    Look at T4 templates or a similar templating solution

    0 讨论(0)
提交回复
热议问题