/// <summary> /// Recupera a chave do template. /// </summary> /// <param name="name"></param> /// <param name="templateType"></param> /// <param name="context"></param> /// <returns></returns> public ITemplateKey GetKey(string name, ResolveType templateType, ITemplateKey context) { if (System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists(name)) { var virtualFile = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(name); var cache = virtualFile as ITemplateCacheSupport; RazorEngine.Templating.ITemplateKey templateKey = null; if (cache != null) { if (cache.TemplateKey == null) { templateKey = new NameOnlyTemplateKey(name, templateType, context); cache.Register(templateKey); } else { templateKey = cache.TemplateKey; } } else { templateKey = new NameOnlyTemplateKey(name, templateType, context); } return(templateKey); } return(new NameOnlyTemplateKey(name, templateType, context)); }
private void CacheTemplates() { lock (typeof(ElasticMailPushService)) { if (!_cached) { var mailModelType = typeof(MailModel); //find all email models in the application mailModelType.Assembly .GetTypes() .Where(_type => _type.Namespace == mailModelType.Namespace) .Where(_type => _type != mailModelType) .ForAll((_cnt, _type) => { //compile and cache the mail models var templateSource = new MailModelTemplateSource(_type); var key = new NameOnlyTemplateKey(templateSource.Name, ResolveType.Global, null); if (!Engine.Razor.IsTemplateCached(key, _type)) { Engine.Razor.Compile(templateSource, templateSource.Name, _type); } }); _cached = true; } } }
private static ITemplateKey CreateRazorKey(string defaultview) { var key = new NameOnlyTemplateKey("spaLayout", ResolveType.Global, null); var rootPath = AppDomain.CurrentDomain.BaseDirectory; var template = File.ReadAllText(Path.Combine(rootPath, defaultview)); Engine.Razor.AddTemplate(key, new LoadedTemplateSource(template)); return(key); }
public void IsolatedRazorEngineService_StaticModelDynamicType_InSandBox() { using (var service = IsolatedRazorEngineService.Create(SandboxCreator)) { const string template = "<h1>Hello @Model.Name</h1>"; const string expected = "<h1>Hello Matt</h1>"; // We "abuse" NameOnlyTemplateKey because it is SecurityTransparent and serializable. var model = new NameOnlyTemplateKey("Matt", ResolveType.Global, null); string result = service.RunCompile(template, "test", null, model); Assert.AreEqual(expected, result); } }
public static string RenderRazorViewToString(string viewName, EmailRequestModel model) { var path = HostingEnvironment.MapPath(viewName); var viewRaw = File.ReadAllText(path); var key = new NameOnlyTemplateKey("EmailReply", ResolveType.Global, null); Engine.Razor.AddTemplate(key, new LoadedTemplateSource(viewRaw)); StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) Engine.Razor.RunCompile(key, sw, null, model); { return(sb.ToString()); } }
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // TemplateEmailService to convert HTML Pages to String Developed by Ravid Yoeun // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //RenderRazorViewToString accepts two params, the directory of the html page, and the request model to be display user info. public string RazorViewToString(string viewName, EmailRequest model) { // RazorEngine code that converts all html code to string // Caution - Please make sure the razor HTML template you reference has inlined-css if being styled or it will not show var path = HostingEnvironment.MapPath(viewName); var viewRaw = File.ReadAllText(path); var key = new NameOnlyTemplateKey("EmailReply", ResolveType.Global, null); Engine.Razor.AddTemplate(key, new LoadedTemplateSource(viewRaw)); StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) Engine.Razor.RunCompile(key, sw, null, model); { return(sb.ToString()); } }
public string RunTemplate(string templateName, Type type, object model, IDictionary <string, object> dynamicViewBag = null) { var templateKey = new NameOnlyTemplateKey(templateName, ResolveType.Layout, null); return(this._razorEngineService.RunCompile(templateKey, type, model, dynamicViewBag != null ? new DynamicViewBag(dynamicViewBag) : null)); }
public void CacheTemplate(string templateName, Type type) { var templateKey = new NameOnlyTemplateKey(templateName, ResolveType.Layout, null); this._razorEngineService.Compile(templateKey, type); }