我有 xsl 视图引擎并想从 xsl 调用标准视图助手(例如 UrlHelper.Action)。但是我未能将参数的可变成员传递给辅助方法。
助手类:
namespace Services
{
public class ViewHelper
{
// ...
public string DummyHelper(params string[] dummyArgs)
{
return String.Concat(dummyArgs);
}
}
}
为 xsl 转换添加助手支持:
var xsl = new XslCompiledTransform();
xsl.Load('MyView.xsl');
varc xsltArgs = new XsltArgumentList();
// Create helper, pass controller context as a param
var helper = new Services.ViewHelper(context));
xslt.AddExtensionObject("urn:helper", helper);
xsl.Transform(xmlDocument, xsltArgs, output);
使用助手的 xsl 脚本之一:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:h ="urn:helper"
exclude-result-prefixes="h msxsl"
>
<xsl:template match="/">
<xsl:value-of select="h:DummyHelper('lorem', 'ipsum', 'dolor', 'sit', 'amet')"/>
</xsl:template>
</xsl:stylesheet>
引发异常并显示消息:找不到具有 5 个参数的 mthod DummyHelper。