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

后端 未结 7 1677
一整个雨季
一整个雨季 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: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(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(sb.ToString(), model);
            }
        }
    }
    

提交回复
热议问题