C#.NET:带有字符引用

时间:2017-06-02 00:13:30

标签: c# asp.net unicode special-characters query-string

我在创建查询字符串并将其发送到其他网页时遇到问题。

我要发送的文字很长,并且有特殊字符。这是一个例子:

通过定义从0到1的间隔作为整体并将其划分为?相等的部分,在数字线图上表示分数1 /。。认识到每个部分的大小为1 /?,并且基于0的部分的端点位于数字行上的数字1 /。。

如果我手动编码,我可以发送此信息:

     <a href="Default.cshtml?standardText=Represent a fraction 1/&#120355; on a number line diagram by defining the interval from 0 to 1 as the whole and partitioning it into &#120355; equal parts. Recognize that each part has size 1/&#120355; and that the endpoint of the part based at 0 locates the number 1/&#120355; on the number line.">
        Link Text
    </a>

这没有任何问题,我可以在另一边读取整个查询字符串。

但是如果我以编程方式创建链接,我的查询字符串会在第一个字符引用之前被截断。我在辅助函数中使用以下设置:

string url = "Default.cshtml";
url += "?standardText=" + standard.text;
<a href="@url">Link Text</a>

当我使用它时,我只得到“将分数理解为1 /”然后停止。

当我查看页面来源时,链接的唯一区别是一个有实际的&符号,第二个是转换为&amp;

<a href="Default.cshtml?standardText=Understand a fraction 1/&amp;#120355; as the quantity formed by 1 part when a whole is partitioned into &amp;#120355; equal parts; understand a fraction &amp;#120354;/&amp;#119887; as the quantity formed by &amp;#120354; parts of size 1/&amp;#120355;."

所以问题不是真正的空间,而是&amp;被解释为启动一个新的查询字符串参数。

我尝试了各种各样的东西[使用HttpUtility.UrlEncode,HttpUtility.UrlEncodeUnicode,Html.Raw,尝试用“+”替换空格],但问题不在于空格,而是在于字符引用是如何形成的处理。当我尝试HttpUtility.urlEncode时,我遇到了双重编码的安全错误。

根据OmG的建议,我尝试使用以下方法替换所有&#s,#s和/ s;

url = url.Replace("&","%26");
url = url.Replace("#","%23");
url = url.Replace("/","%2F");

这导致以下链接:

    <a href="Default.cshtml?standardText=Understand a fraction 1%2F%26%23120355; as the quantity formed by 1 part when a whole is partitioned into %26%23120355; equal parts; understand a fraction %26%23120354;%2F%26%23119887; as the quantity formed by %26%23120354; parts of size 1%2F%26%23120355;.">All Items</a>

现在当我点击链接时,我收到了不同的安全警告/错误:

从客户端检测到一个潜在危险的Request.QueryString值(standardText =“... raction 1 /?as qua ...”)。

我不明白为什么通过QueryString发送字符引用是如此困难。有没有办法阻止Razor将我的所有&amp; s转换为&amp; amp; amp; amp; amp; amp; amp; ?当它只是简单的“&amp;”时,地址工作正常。

更新:在字符串上使用URLDecode()不会影响其字符实体引用,所以当我尝试解码字符串然后重新编码时,我仍然会得到双逃避安全警告。

更新:根据@MikeMcCaughan的建议,我尝试使用JS,但我不太了解混合JS和Razor。我尝试通过将脚本放入正文来创建链接,如下所示:

    <script type="text/javascript">

        var a = document.createElement('a');
        var linkText = document.createTextNode("my title text");
        a.appendChild(linkText);
        a.title = "my title text";
        a.href = encodeURIComponent(@url);
        document.body.appendChild(a);

    </script>

但是没有出现任何链接,所以我显然做错了。

供参考,当我尝试使用@ Html.Raw(url)时,

<a href="@Html.Raw(url)">Link Text</a>

&amp; s仍然变成&amp; amp; s。链接呈现为:

<a href="Player.cshtml?standardText=Represent a fraction 1/&amp;#120355; on a number line diagram by defining the interval from 0 to 1 as the whole and partitioning it into &amp;#120355; equal parts. Recognize that each part has size 1/&amp;#120355; and that the endpoint of the part based at 0 locates the number 1/&amp;#120355; on the number line.">Link text</a>

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是用编码替换特殊字符,可以从here访问。

如您所见,使用&替换字符%26 .replace string。另外,将/替换为%2F#替换为%23;替换为%3B,空格替换为%20

此外,您可以通过以下函数在C#中执行这些操作:

Server.URLEncode("<The Url>")

并在Javascript中通过以下函数:

encodeURI("<The Url>")

另外,如您所知,双重编码为​​this。为了防止双重编码,在将字符串传递给Server.URLEncode函数之前,您应该没有编码字符串的某些部分。