我在ASP.NET MVC應用程序中使用NVelocity時遇到了一些困難。我使用它作爲生成電子郵件的一種方式。NVelocity找不到模板
據我可以弄清楚我傳遞的細節都是正確的,但它無法加載模板。
下面是代碼:
private const string defaultTemplatePath = "Views\\EmailTemplates\\";
...
velocityEngine = new VelocityEngine();
basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, defaultTemplatePath);
ExtendedProperties properties = new ExtendedProperties();
properties.Add(RuntimeConstants.RESOURCE_LOADER, "file");
properties.Add(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, basePath);
velocityEngine.Init(properties);
的基本路徑是正確的目錄,我已經粘貼值轉換成資源管理器,以確保它是正確的。
if (!velocityEngine.TemplateExists(name))
throw new InvalidOperationException(string.Format("Could not find a template named '{0}'", name));
Template result = velocityEngine.GetTemplate(name);
「名稱」是以上所定義如上基本路徑的文件夾中有效的文件名。但是,TemplateExists返回false。如果我評論說,條件,讓其失敗的是getTemplate方法調用堆棧跟蹤看起來是這樣的:
at NVelocity.Runtime.Resource.ResourceManagerImpl.LoadResource(String resourceName, ResourceType resourceType, String encoding)
at NVelocity.Runtime.Resource.ResourceManagerImpl.GetResource(String resourceName, ResourceType resourceType, String encoding)
at NVelocity.Runtime.RuntimeInstance.GetTemplate(String name, String encoding)
at NVelocity.Runtime.RuntimeInstance.GetTemplate(String name)
at NVelocity.App.VelocityEngine.GetTemplate(String name)
...
我現在是在有點陷入僵局。我覺得答案是非常明顯的,但我目前似乎無法看到它。
難道我違背了一些不成文的規則,即ASP.NET MVC的標籤(等等)移除? – 2009-12-21 14:36:58
我刪除了asp.net標籤,因爲這完全與asp.net無關。 – 2009-12-21 16:26:08
嗯...... ASP.NET標籤仍然存在。你的意思是ASP.NET MVC。如果它與ASP.NET MVC沒有關係,它又如何與ASP.NET相關? – 2009-12-22 15:04:37