如何在Azure API管理(C#)策略表达式中应用HTML解码?

如何在Azure API管理(C#)策略表达式中应用HTML解码?,c#,azure-api-management,C#,Azure Api Management,我在HTTP POST正文中有有效负载,在转发到后端之前,我需要对特定字段应用HTML解码。在System.Web.HttpUtility.HtmlCode似乎不可用的API管理策略表达式中,如何实现这一点 尝试使用自制版本失败,因为策略编辑器翻译了ä至ä: <set-body>@{ string HtmlDecode(string input) => input.Replace("&auml;","ä"); var body = context

我在HTTP POST正文中有有效负载,在转发到后端之前,我需要对特定字段应用HTML解码。在System.Web.HttpUtility.HtmlCode似乎不可用的API管理策略表达式中,如何实现这一点

尝试使用自制版本失败,因为策略编辑器翻译了
ä
ä

<set-body>@{
    string HtmlDecode(string input) => input.Replace("&auml;","ä");
    var body = context.Request.Body.As<JObject>(true);
    body["field1"] = HtmlDecode(body["field1"].ToString());
    return body.ToString();
}</set-body>
@{
字符串HtmlDecode(字符串输入)=>input.Replace(“ä;”,“ä”);
var body=context.Request.body.As(true);
body[“field1”]=HtmlDecode(body[“field1”].ToString());
返回body.ToString();
}

不是我的首选和预期解决方案,而是在@Dana和Maxim Kim(API管理团队)的帮助下,解决方案:

<set-body>@{
    Dictionary<string,string> decoderPairs = new Dictionary<string,string>() 
    {
        {"&amp;auml;","ä"},
        {"&amp;ouml;","ö"},
        {"&amp;uuml;","ü"},
        {"&amp;Auml;","Ä"},
        {"&amp;Ouml;","Ö"},
        {"&amp;Uuml;","Ü"},
        {"&amp;szlig;","ß"},
        {"&amp;amp;","&"}
    };                
    string HtmlDecode(string input) { foreach(var p in decoderPairs) { input = input.Replace(p.Key,p.Value); } return input; }
    var body = context.Request.Body.As<JObject>(true);
    body["field1"] = HtmlDecode((body["field1"] ?? "").ToString());
    return body.ToString();
}</set-body>
@{
Dictionary decoderPairs=新字典()
{
{&;auml;,“ä”},
{&;ouml;,“ö”},
{&;uuml;,“ü”},
{&;Auml;,“Ä”},
{&;Ouml;,“Ö”},
{&;Uuml;,“Ü”},
{&;szlig;,“ß”},
{amp;amp;amp;,“&”}
};                
字符串HtmlDecode(字符串输入){foreach(decoderPairs中的var p){input=input.Replace(p.Key,p.Value);}返回输入;}
var body=context.Request.body.As(true);
body[“field1”]=HtmlDecode((body[“field1”]?“”)。ToString();
返回body.ToString();
}
编辑 既然这样,就有了合适的解决方案

<set-body>@{
    var body = context.Request.Body.As<JObject>(true);
    body["field1"] = System.Net.WebUtility.HtmlDecode((body["field1"] ?? "").ToString());
    return body.ToString();
}</set-body>
@{
var body=context.Request.body.As(true);
body[“field1”]=System.Net.WebUtility.HtmlDecode((body[“field1”]??“”).ToString();
返回body.ToString();
}

不是我的首选和预期解决方案,而是在@Dana和Maxim Kim(API管理团队)的帮助下,解决方案:

<set-body>@{
    Dictionary<string,string> decoderPairs = new Dictionary<string,string>() 
    {
        {"&amp;auml;","ä"},
        {"&amp;ouml;","ö"},
        {"&amp;uuml;","ü"},
        {"&amp;Auml;","Ä"},
        {"&amp;Ouml;","Ö"},
        {"&amp;Uuml;","Ü"},
        {"&amp;szlig;","ß"},
        {"&amp;amp;","&"}
    };                
    string HtmlDecode(string input) { foreach(var p in decoderPairs) { input = input.Replace(p.Key,p.Value); } return input; }
    var body = context.Request.Body.As<JObject>(true);
    body["field1"] = HtmlDecode((body["field1"] ?? "").ToString());
    return body.ToString();
}</set-body>
@{
Dictionary decoderPairs=新字典()
{
{&;auml;,“ä”},
{&;ouml;,“ö”},
{&;uuml;,“ü”},
{&;Auml;,“Ä”},
{&;Ouml;,“Ö”},
{&;Uuml;,“Ü”},
{&;szlig;,“ß”},
{amp;amp;amp;,“&”}
};                
字符串HtmlDecode(字符串输入){foreach(decoderPairs中的var p){input=input.Replace(p.Key,p.Value);}返回输入;}
var body=context.Request.body.As(true);
body[“field1”]=HtmlDecode((body[“field1”]?“”)。ToString();
返回body.ToString();
}
编辑 既然这样,就有了合适的解决方案

<set-body>@{
    var body = context.Request.Body.As<JObject>(true);
    body["field1"] = System.Net.WebUtility.HtmlDecode((body["field1"] ?? "").ToString());
    return body.ToString();
}</set-body>
@{
var body=context.Request.body.As(true);
body[“field1”]=System.Net.WebUtility.HtmlDecode((body[“field1”]??“”).ToString();
返回body.ToString();
}

由于API管理策略表达式支持XDocument,您可以使用它来解码大多数html/xml数据块:


字符串解码HTML(字符串值){if(value==null)返回null;返回XDocument.Parse($“{value}”).Root.value;}

由于API管理策略表达式支持XDocument,您可以使用它来解码大多数html/xml数据块:


字符串解码HTML(字符串值){if(value==null)返回null;返回XDocument.Parse($“{value}”).Root.value;}

现在您甚至不必使用c来解码HTML转义字符串。只需直接使用System.Net.WebUtility.htmlde代码:

<set-body>@(System.Net.WebUtility.HtmlDecode(escaped_string))</set-body>
@(System.Net.WebUtility.HtmlDecode(转义字符串))

现在您甚至不必使用c来解码HTML转义字符串。只需直接使用System.Net.WebUtility.htmlde代码:

<set-body>@(System.Net.WebUtility.HtmlDecode(escaped_string))</set-body>
@(System.Net.WebUtility.HtmlDecode(转义字符串))

您有权访问
System.Net.WebUtility
命名空间吗?不幸的是,没有,当尝试System.Net.WebUtility.HtmlCode()时,我得到:表达式中不支持使用“System.Net.WebUtility”类型。此功能看起来确实缺失。您可以尝试转义与
.Replace(“\u0026auml;”,“ä”)
,但您必须执行大量的查找/替换操作。谢谢@dana,在最终解决方案出现之前,我将尝试此方法作为一种变通方法。您是否有权访问
System.Net.WebUtility
命名空间?不幸的是,没有,当尝试System.Net.WebUtility.HtmlCode()时,我得到:表达式中不支持使用“System.Net.WebUtility”类型。此功能看起来确实缺失。您可以尝试转义与
.Replace(“\u0026auml;”,“ä”)
,但您必须执行大量查找/替换操作。谢谢@dana,在最终解决方案出现之前,我将尝试使用此方法