.net 4 xslt transformation Extension Function broken

前端 未结 1 1353
余生分开走
余生分开走 2021-01-21 17:41

I\'m in the process of upgrading an asp.net v3.5 web app. to v4 and I\'m facing some problems with XSLT transformations I use on XmlDataSource objects.

Part of a XSLT fi

相关标签:
1条回答
  • 2021-01-21 18:06

    The problem seems to be that .NET 4.0 introduces an additional overload for HttpUtility.HtmlEncode. Up to .NET 3.5 there were the following overloads:

    public static string HtmlEncode(string s);
    public static void HtmlEncode(string s, TextWriter output);
    

    .NET 4.0 also has the following method:

    public static string HtmlEncode(object value);
    

    This results in an XslTransformException:

    (Ambiguous method call. Extension object 'ds:HttpUtility' contains multiple 'HtmlEncode' methods that have 1 parameter(s).

    You probably don't see the exception because it is caught somewhere and not immediately reported.

    Using .NET Framework classes as extension objects is a fragile thing as a new Framework version might break your transformation.

    A fix would be to create a custom wrapper class and use that as extension object. This wrapper class may not have overloads with the same number of parameters:

    class ExtensionObject
    {
        public string HtmlEncode(string input)
        {
            return System.Web.HttpUtility.HtmlEncode(input);
        }
    }
    
    //...
    XsltArgumentList arguments = new XsltArgumentList();
    arguments.AddExtensionObject("my:HttpUtility", new ExtensionObject());
    
    0 讨论(0)
提交回复
热议问题