NVelocity没有找到模板 [英] NVelocity not finding the template

查看:212
本文介绍了NVelocity没有找到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些困难,在一个ASP.NET MVC应用程序中使用NVelocity。我用它作为生成电子邮件的方式。

I'm having some difficulty with using NVelocity in an ASP.NET MVC application. I'm using it as a way of generating emails.

据我可以做出来,我路过是正确的细节,但它无法加载模板。

As far as I can make out the details I'm passing are all correct, but it fails to load the template.

下面是code:

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);

的基本路径是正确的目录中,我已经粘贴值存入资源管理器,以确保它是正确的。

The basePath is the correct directory, I've pasted the value into explorer to ensure it is correct.

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方法调用堆栈跟踪看起来是这样的:

'name' above is a valid filename in the folder defined as basePath above. However, TemplateExists returns false. If I comment that conditional out and let it fail on the GetTemplate method call the stack trace looks like this:

   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)
...

我现在是在位的僵局。我觉得答案是一清二楚的,但我似乎无法看到它的那一刻。

I'm now at a bit of an impasse. I feel that the answer is blindingly obvious, but I just can't seem to see it at the moment.

推荐答案

好吧 - 所以我设法得到的东西的工作,但它是一个黑客位的,而不是接近,我想一个解决方案的任何地方,但它得到的东西的工作。

Okay - So I'm managed to get something working but it is a bit of a hack and isn't anywhere near a solution that I want, but it got something working.

基本上,我手工模板加载到一个字符串,然后将字符串传递给它把结果写入到给定的StringWriter的velocityEngine.Evaluate()方法。这样做的副作用是,在模板中的#parse指令不工作,因为它仍然无法找到这些文件。

Basically, I manually load in the template into a string then pass that string to the velocityEngine.Evaluate() method which writes the result into the the given StringWriter. The side effect of this is that the #parse instructions in the template don't work because it still cannot find the files.

using (StringWriter writer = new StringWriter())
{
    velocityEngine.Evaluate(context, writer, templateName, template);
    return writer.ToString();
}

在,因为它不使用code以上TEMPLATENAME是无关紧要的。模板是包含已经从磁盘$ P $对装载。整个模板字符串

In the code above templateName is irrelevant as it isn't used. template is the string that contains the entire template that has been pre-loaded from disk.

我还是AP preciate因为我没有更好的办法真的不喜欢这一点。

I'd still appreciate any better solutions as I really don't like this.

这篇关于NVelocity没有找到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆