0

How to pass special characters e.g. '+' or '#' at the query string?

I am calling an MVC action and passing the value "abc+def" as a query string, but while I was debugging I noticed that the passed value is "abc def" (the '+' character was replaced by space!)

This is the code of the action

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

And this is the JS line of code that calls the previous action

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

The value that I wrote at the text input was "abc+def" but the value that passed to the server is "abd def" And the url at the browser is: "/Books/Index?textSearch=abc+def"

How to fix this issue??

1 Answers1

4

You need to call encodeURIComponent() to escape it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964