Recover value from an HTTP query string

Asked

Viewed 4,300 times

2

I have a URL where it returns an HTTP QueryString, and I need to retrieve the information from the URL and convert it into variables to make queries in the database. In PHP just use the $_GET[""], now in C# I don’t know how to proceed.

Let me give you an example to clarify.

I have the URL: http://html.net/page.php?name=Joe&age=12

I have to take the values "Joe" and "12" for a later consultation.

1 answer

3


Try this and see if it solves.

string nome = Request.QueryString["name"];

string idade = Request.QueryString["age"];
  • Do not forget to check that the parameter is null before using and do not use sensitive parameters in the query string. Ex: login, password, logged in user id, Session, any control parameter that can be used to take over a session without necessarily knowing any login.

  • I added this code to my controller, but when calling the page, the null variable.

  • public Actionresult Dependent() { string name = Request.Querystring["name"]; var dependent = dependentRepository.Dependentes.Where(r => r.Nmpessoa == name). Tolist(); Return View(dependent); } And am I trying to recover the value of this URL: http://localhost/User/Dependent? name=Joao

  • It worked, I forgot to put the parameters in my controller. Thank you all.

  • I have to put the amount of parameters I will receive in my Query String in my controller, correct?

Browser other questions tagged

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