Engine.Razor.Run 导致 InvalidCastException

Engine.Razor.Run causes InvalidCastException

我有以下代码来获取我的 cshtml 电子邮件模板。

try
            {

            var layoutTemplateKey = new NameOnlyTemplateKey("layout", ResolveType.Global, null);
            if (!Engine.Razor.IsTemplateCached(layoutTemplateKey, null))
                Engine.Razor.AddTemplate(layoutTemplateKey, ReadTemplateFile("/Views/Email/Layout.cshtml"));

            var templateKey = new NameOnlyTemplateKey(templatePath, ResolveType.Global, null);
            if (!Engine.Razor.IsTemplateCached(templateKey, viewModel.GetType()))
            {
                Engine.Razor.AddTemplate(templateKey, ReadTemplateFile(templatePath));
                Engine.Razor.Compile(templateKey, viewModel.GetType());
            }

            return viewModel == null ?
                Engine.Razor.Run(templateKey) :
                Engine.Razor.Run(templateKey, viewModel.GetType(), viewModel);

            }
            catch (Exception ex)
            {
                return ex.Message;
            }

然而,我的 Engine.Razor.Run 导致 InvalidCastException 使用此处未引用的模型。

并且它也不断发生在任何其他模型上。

我最近所做的更改是由另一个模型继承每个 class 以用于 Layout.cshtml

预期模型:NewBookRecievedEmailViewModel

public class NewBookRecievedEmailViewModel : EmailHelperModel
    {
        public ProposalPostedEmailViewModel()
        {
            this.canBeDeleted = true;
        }

        public string BookName { get; set; }

        public string Author {get; set;}
    }

模型 PayOutBookEmailViewModel 出错

public class PayOutBookEmailViewModel : EmailHelperModel
    {
        public string BookID { get; set; }

        public string BookUrl {get; set;}
    }

继承的模型是:EmailHelper

public class EmailHelperModel
    {
        public bool canBeDeleted { get; set;  }
        public string DeleteUrl { get; set; }
    }

堆栈跟踪:

   at RazorEngine.Templating.TemplateBase`1.SetModel(Object model)
   at RazorEngine.Templating.TemplateBase.SetData(Object model, DynamicViewBag viewbag)
   at RazorEngine.Templating.RazorEngineCore.CreateTemplate(ICompiledTemplate template, Object model, DynamicViewBag viewbag)
   at RazorEngine.Templating.RazorEngineCoreWithCache.ResolveInternal(String cacheName, Object model, Type modelType, DynamicViewBag viewbag, ResolveType resolveType, ITemplateKey context)
   at RazorEngine.Templating.InternalTemplateService.Resolve(String name, Object model, Type modelType, DynamicViewBag viewbag, ResolveType resolveType)
   at RazorEngine.Templating.TemplateBase`1.ResolveLayout(String name)
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
   at RazorEngine.Templating.RazorEngineCore.RunTemplate(ICompiledTemplate template, TextWriter writer, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineService.Run(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.DynamicWrapperService.Run(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass22_0.<Run>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at RazorEngine.Templating.RazorEngineServiceExtensions.Run(IRazorEngineService service, ITemplateKey key, Type modelType, Object model, DynamicViewBag viewBag)
   at Pxp.Api.Helpers.EmailHelper.GetHtmlContent(String templatePath, Object viewModel)

考虑使用实际的 HTML,而不是使用 cshtml;或者更好的是,使用 JavaScript 生成元素,这是真正的方法,这里是一些如何在 JavaScript:

中创建元素的示例代码
var MyEngine = new (function() {
     this.el = function(opts) {
        if(!svgList.split(" ").find(x => x == opts.tag)) {
            this.node = document.createElement(opts.tag || "div");
        } else {
            this.node = document.createElementNS('http://www.w3.org/2000/svg', opts.tag);
        } 

        for(var k in opts) {
            if(k == "style") {
                for(var s in opts[k]) {
                    this.node.style[s] = opts[k][s];
                }
            } else if(k != "parent"){
                this.node[k] = opts[k];
            }
        }



        this.setAttrs = (attrs) => {
            for(var k in attrs) {
                this.node.setAttribute(k, attrs[k]);
            }
        };
        this.getAttr = (at) => {
            return this.node.getAttribute(at);
        };

        this.setStyle = (stl) => {
            for(var k in stl) {
                this.node.style[k] = stl[k];
            }
        }

        var attr = opts.attr || {};
        this.setAttrs(attr);
        var optsPar = opts.parent;
        var par = null;
        if(optsPar) {
            if(optsPar.constructor == String) {
                par = f("#" + optsPar);
            } else if(optsPar instanceof Element) {
                par = optsPar;
            }
        }

        this.parent = par || document.body || {appendChild: (d) => {}};
        this.parent.appendChild(this.node);
     };
)();

然后创建一个新元素:

var temp = new MyEngine.el({
    id:"hi",
    style:{position:absolute, left:100+"px"},
    innerHTML:"<b>nice to meet you</b>
});