RazorEngine and parsing physical view files always causes exception

前端 未结 3 424
悲&欢浪女
悲&欢浪女 2021-02-02 18:10

I have the following RazorEngine call:

public class RazorEngineRender
{
    public static string RenderPartialViewToString(string templatePath, string viewName,          


        
3条回答
  •  孤城傲影
    2021-02-02 18:15

    The accepted answer was perfect in 2011 (I believe pre-v3 of RazorEngine) but this code is now marked as obsolete in latest version (in time of typing it is 3.7.3).

    For newer version your method can be typed like this:

    public static string RenderPartialViewToString(string templatePath, string templateName, string viewName, T model)
            {
                string template = File.ReadAllText(Path.Combine(templatePath, viewName));
                string renderedText = Engine.Razor.RunCompile(template, templateName, typeof(T), model);
                return renderedText;
            }
    

    and in order for it to work you need to add

    using RazorEngine.Templating;
    

提交回复
热议问题