我在 ASP.NET MVC 应用程序中使用 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);
basePath 是正确的目录,我已将该值粘贴到资源管理器中以确保它是正确的。
if (!velocityEngine.TemplateExists(name))
throw new InvalidOperationException(string.Format("Could not find a template named '{0}'", name));
Template result = velocityEngine.GetTemplate(name);
上面的“name”是上面定义为 basePath 的文件夹中的有效文件名。但是,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)
...
我现在有点陷入僵局。我觉得答案是显而易见的,但我现在似乎看不到它。