What is the difference between HttpPost and HttpGet in MVC 5?

1

What is the difference between using [HttpGet] and [HttPost] , when should one be used and when the other?

    
asked by Eduardo Enrique Moreno Caldero 05.04.2017 в 04:27
source

3 answers

1

Well understanding your question, about the use and difference between POST and GET , which was what I saw from the beginning when I saw your question.

In the case of the POST method, which is used to make a request to a server sending information, and is to insert or consult something and wait for a response after the end of the operation to receive some type of message or information. For example I use the POST to be able to log in to a mobile application, I send the user and the password to a server waiting for the answer if the user and the password exist.

In the case of GET it is used to obtain information, for example if what you are going to do is send an array of information after having updated something on the server side, for example, when after connect to your phone you get messages without sending any kind of information.

    
answered by 05.04.2017 / 05:40
source
1

It is an attribute whose purpose is to establish the way in which the action can be accessed; that is, if you use the one that has GET, it will only "find" it when the call method was GET, in the case of POST it is similar when the invocation of an action is by POST.

Example if you have in your Controller

[HttpPost]
public ActionResult Mifuncion() {
  // Tu código
}

using Ajax will return error if you use:

$.ajax({
   url : "ruta/Mifuncion",
   method : 'GET',
   /* TU DEMÁS CÓDIGO */
}
);

otherwise (it will be successful) if you do it in the following way:

$.ajax({
   url : "ruta/Mifuncion",
   method : 'POST',
   /* TU DEMÁS CÓDIGO */
}
);

will conclude successfully.

    
answered by 05.04.2017 в 04:54
1

If you mean the differences between one and the other:

Information taken from w3schools.com .

At what point should you use one or the other? It depends on the objective of your test, but if you rely on the previous table you will surely always choose the correct method.

    
answered by 05.04.2017 в 05:24