在一次NVelocity项目开发中,出现了Unable to find resource ... 异常,经分析是模板文件路径问题。
解决方案:
在模板引擎环境初始化之前,设置好文件路径。
_Props.AddProperty("file.resource.loader.path", System.Web.HttpContext.Current.Server.MapPath("~"))
站点主目录路径:
System.Web.HttpContext.Current.Server.MapPath("~")
当前文件夹路径:
System.Web.HttpContext.Current.Server.MapPath(".")
示例代码:
'''
<summary>
''' 初始化VelocityEngine
''' </summary>
Private Sub VelocityInit()
_Velocity = New Velocity()
_Props = New ExtendedProperties()
_Props.AddProperty(RuntimeConstants.RESOURCE_LOADER, " file " )
_Props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath))
_Props.AddProperty(RuntimeConstants.INPUT_ENCODING, " gb2312 " )
_Props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, " gb2312 " )
_Props.AddProperty( " file.resource.loader.path " , System.Web.HttpContext.Current.Server.MapPath( " ~ " ))
Velocity.Init(_Props)
End Sub
''' 初始化VelocityEngine
''' </summary>
Private Sub VelocityInit()
_Velocity = New Velocity()
_Props = New ExtendedProperties()
_Props.AddProperty(RuntimeConstants.RESOURCE_LOADER, " file " )
_Props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath))
_Props.AddProperty(RuntimeConstants.INPUT_ENCODING, " gb2312 " )
_Props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, " gb2312 " )
_Props.AddProperty( " file.resource.loader.path " , System.Web.HttpContext.Current.Server.MapPath( " ~ " ))
Velocity.Init(_Props)
End Sub