Recover query string with "+" character

Asked

Viewed 1,363 times

1

I have in my controller, a method where I take a Query String, I decrypt it and use the data in my query. However, when the encrypted value has a +, he is not recovered by Request.QueryString. Soon I get an error when 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();
        }

Example of parameter passed: ./?param=kmFxK8ID3ç+Zdggbqn9oja==
What is recovered: ./?param=kmFxK8ID3ç Zdggbqn9oja==

The sign of + vanish, and there’s a space in place.

  • You want to recover the signal in the param? Ex: param=foo+bar, is to return foo+bar?

  • @Laerte edited the question with what I need. That’s exactly it. " foo+bar"

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 the + by the Percent-encoding of it: %2B

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

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

string param = Request.QueryString["param"];
string nome = param != null ? HttpUtility.UrlDecode(param.Replace(" ", "%2B")) : "";
  • The problem is that this Query String is being passed to me by another application, which I don’t have access to. There is no way I can handle this in my controller no?

  • I supplemented the question, check if it solves your problem.

  • The string name appears the +sign, but when using the Url.Decode, it comes back as space again.

  • You just need the sign of +, right?

  • I just need to replace the space by the sign of +

  • Okay, I also put a check on the stop is void.

  • That’s just what I needed. Thanks for your help.

Show 2 more comments

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.