我最近升級到RazorEngine(3.6.1)的最新穩定版本,我的用於清除策略由於所有更改,緩存不再有效。很多內容已經改變,這個項目的文檔不僅過時了,而且從作者的角度出發,造成了糟糕的用戶體驗。
這是我使用3.6.1清除所有緩存模板的當前代碼。
public static class TemplateManager
{
static IRazorEngineService Service { get; set; }
static TemplateServiceConfiguration Configuration { get; set; }
static TemplateManager()
{
Configuration = new TemplateServiceConfiguration()
{
// setting up our custom template manager so we map files on demand
TemplateManager = new MyTemplateManager()
};
Service = RazorEngineService.Create(Configuration);
Engine.Razor = Service;
}
/// <summary>
/// Resets the cache.
/// </summary>
public static void ResetCache()
{
Configuration.CachingProvider = new RazorEngine.Templating.DefaultCachingProvider();
}
/// <summary>
/// Compiles, caches and parses a template using RazorEngine.
/// </summary>
/// <param name="templateType">Type of the template.</param>
/// <param name="anonymousType">Type of the anonymous object.</param>
/// <param name="cachedEnabled">true to enabled caching; false otherwise</param>
/// <returns></returns>
public static string GetTemplate<T>(EmailTemplateType templateType, T anonymousType, bool cachedEnabled = true)
{
string templateName = templateType.ToString();
if (cachedEnabled == false)
ResetCache();
// pre-compile, cache & parse the template
return Engine.Razor.RunCompile(templateName, null, anonymousType);
}
}
public enum EmailTemplateType
{
ForgotPassword,
EmailVerification
}
public class MyTemplateManager : ITemplateManager
{
public ITemplateSource Resolve(ITemplateKey key)
{
string file = HttpContext.Current.Server.MapPath(string.Format("~/EmailTemplates/{0}.cshtml", key.Name));
return new LoadedTemplateSource(System.IO.File.ReadAllText(file), file);
}
public ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context)
{
return new NameOnlyTemplateKey(name, resolveType, context);
}
public void AddDynamic(ITemplateKey key, ITemplateSource source)
{
throw new NotImplementedException("dynamic templates are not supported!");
}
}
這是Asp.Net MVC的代碼示例用法:
var emailBody = TemplateManager.GetTemplate(EmailTemplateType.ForgotPassword, new
{
SiteUrl = Url.Action(MVC.Home.Index(), protocol: Request.Url.Scheme),
SiteFriendlyName = SiteSettings.Instance.DomainName.FriendlyName,
PasswordResetLink = Url.Action(MVC.Account.ActionNames.ResetPassword, MVC.Account.Name, new { userId = user.Id, code = code }, protocol: Request.Url.Scheme),
NotRequestedUrl = Url.Action(MVC.Account.ActionNames.PasswordResetNotReqeuested, MVC.Account.Name, new { userId = user.Id, requesterIpAddress = WebUtils.GetClientIPAddress(), code = code }, protocol: Request.Url.Scheme)
},
/* this setting allows me to disable caching during development */
!SiteSettings.Instance.EmailSettings.DebugEmailTemplates);
// I could also have a button on an admin page that executed this code to manually reset the cache in production.
TemplateManager.ResetCache();
我的情況有點不同,我想模板(emailBody)返回從控制器用戶所以它會在頁面上可見。我怎樣才能將它作爲視圖返回? – user2818430
@tugboatcaptain我試過你的解決方案,並得到錯誤'在RazorEngine.dll中發生類型'System.ArgumentException'的異常,但沒有在用戶代碼中處理。附加信息:模擬的無效標記 - 不能重複。我所做的就是從數據庫中獲取模板。 – dev
很好的回答。 Thx的幫助! –