【问题标题】:Passing params through URL - Single quotes become '通过 URL 传递参数 - 单引号变为 '
【发布时间】:2021-09-20 17:08:04
【问题描述】:

我正在处理一个 ASP.NET 项目。我们通过 url 传递参数,每次传递单引号时,url 将所有单引号更改为 %27,通过 javascript 读取的实际值将所有单引号更改为 '

谁能告诉我如何维护单引号,因为我们的参数需要匹配确切的值。这是我的代码。

public class Model
{
   public string example {get; set;}
}

public ActionResult Index(string example)
{
   var model = new Model();
   model.example = example;
   return View(model);
}

Index.cshtml at the bottom ---------------------

<script type="text/javascript">
   var example = "@Model.example";
   Main();
</script>

Javascript -----------------

console.log(example);

示例:www.example.com?example=Turtle's_Are_Cool 立即将 url 更改为 => www.example.com?example=Turtle\%27s_Are_Cool 并且 JavaScript 输出 Turtle's_Are_Cool

【问题讨论】:

  • 你应该检查这个类似的问题ASP.NET single quotes are converted to &#39;
  • 在 C# 调试器中,转到Immediate Window 并输入?example. 结果显示确切。还向我们展示生成的确切源(在浏览器中查看源)。

标签: javascript c# asp.net .net


【解决方案1】:

如果您想在服务器端执行此操作,您可以使用 Server.URLEncode("Turtle's_Are_Cool"))

如果您想在客户端管理相同的内容,可以将 ' 替换为 '

example = example.replace(/'/g, "\'");

但是如果你有一个来自服务器端的单引号,并且你想在客户端转换它,那么最简单的方法是使用 HTML 元素,如下面的问题所示

Unescape apostrophe (&#39;) in JavaScript?

【讨论】:

  • 这在某种意义上是有效的,它使得 ' 现在被替换为 %27 而不是 ',但是客户端仍然在努力解决这种表示
  • 在这种情况下,在客户端只需使用 unescape('%27') //这应该给你单引号
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2022-01-22
  • 2023-04-02
相关资源
最近更新 更多