在查询字符串中传递“+”

Passing '+' At the query-string

如何传递特殊字符,例如查询字符串中的“+”或“#”?

我正在调用 MVC 操作并将值 "abc+def" 作为查询字符串传递,但在调试时我注意到传递的值为 "abc def"(“+”字符已被替换通过 space!)

这是动作的代码

public ActionResult Index(string textSearch)
{
    //...
}

这是调用上一个动作的JS代码行

window.location.href = "/Books/Index?textSearch=" + txtBooksFilter.val();

我在文本输入中写入的值是 "abc+def" 但传递到服务器的值是 "abd def" 浏览器的 url 是:“/Books/Index?textSearch=abc+def”

如何解决这个问题?

您需要调用 encodeURIComponent() 进行转义。