Retrieve query string with character "+"

1

I have in my controller, a method where I get a Query String , decrypt it and use the data in my query. However, when the encrypted value has a + sign, it is not recovered by Request.QueryString . I soon get an error trying to decrypt.

Follow my controller:

 public ActionResult 
       {

            //Recebe a string Criptografada
            string param = Request.QueryString["param"];// Nesta Parte o sinal de + não é recuperado
            string nome = HttpUtility.UrlDecode(param);

            //Instancia obj da DLL
            Criptografia cr = new Criptografia();
            //Descriptografa a string
            string nomeDescrypt = cr.Decrypt(nome);// recebo o erro

            //Separa a string por meio do "|"
            string[] stringSeparators = new string[] { "|" };
            var result = nomeDescrypt.Split(stringSeparators, StringSplitOptions.None);

            //Converte para o tipo de dado correto
            var matricula = Convert.ToInt32(result[0]);
            var contrato = Convert.ToInt32(result[1]);

            var usuarios = usuarioRepository.Lista.Where(r => r.CdMatricula == matriculaUser && r.SqContrato == contratoUser).FirstOrDefault();

            return View();
        }

What is retrieved: Parameter example passed: ./?param=kmFxK8ID3ç ZdGGbQN9oJA ==

The sign of + disappears, and space is left in place.

    
asked by anonymous 06.02.2015 / 13:00

2 answers

1

+ has a semantic meaning in the query string. It is used to represent space.

If you want to get the literal + in your query string, you need to change + by its percent-encoding: %2B

Example: ./? param = foo% 2Bbar
In your code behind will return: foo+bar

Another way is to replace + with %2B via programming:

string param = Request.QueryString["param"];
string nome = param != null ? HttpUtility.UrlDecode(param.Replace(" ", "%2B")) : "";
    
06.02.2015 / 13:17
0

You can use the replace() method to replace this blank space!

    
18.02.2015 / 20:46