How to put two actions with HTTPPOST

-2

How to put two controllers with HTTPPOST

[HttpPost]
public ActionResult Index(string Nomel, string Utilizador, string Password, string PasswordC)
{

}

 [HttpPost]
 public ActionResult Index(string Nome, string Email, string Message)
 {
 }
  

The current request for action 'Index' on controller type   'HomeController' is ambiguous between the following action methods:   System.Web.Mvc.ActionResult Index (System.String, System.String,   System.String, System.String) on type Cinel.Controllers.HomeController   System.Web.Mvc.ActionResult Index (System.String, System.String,   System.String) on type Cinel.Controllers.HomeController

    
asked by anonymous 09.03.2018 / 12:55

3 answers

0

In the declaration of Controller use RoutePrefix , would be asism:

[RoutePrefix("api/service")]
public class ServiceController : ApiController

In the functions you should declare as follows:

[Route("enviar")]
public ActionResult Enviar(string Nome, string Email, string Message)
{
   // seu codigo
}

[Route("receber")]
public ActionResult Receber(string Nome, string Email, string Message)
{
  // seu codigo
}

In this way what will define whether it is a Post or Get is the use of the parameters.

This format also accepts parameters in the route, it would look like this:

[Route("receber/{id:string}")]
    
09.03.2018 / 13:29
1

See what the error says (highlighted by me):

  

The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods : System.Web.Mvc.ActionResult Index (System.String, System.String, System .String, System.String) on type Cinel.Controllers.HomeController System.Web.Mvc.ActionResult Index (System.String, System.String, System.String) on type Cinel.Controllers.HomeController

You can not have two actions with the same name this way.

Because when you make a call the application will not know whether to call the first action with the last parameter as null or if it calls the second action.

You just need to use a different name for your actions or define different routes for them.

    
09.03.2018 / 13:01
1

The problem is related to ActionResult does not accept methods with the same name, because this class is related to your view, and the same thing to say that there are 2 files with the same name in the same place.

class Index is just to load the content of the page and does not serve as a get method to be called all the time

Correction:

public ActionResult Index()
{
 return view()
}

 [HttpPost]
 public ActionResult Enviar(string Nome, string Email, string Message)
 {
   // seu codigo
 }

 [HttpGet]
 public ActionResult Receber(string Nome, string Email, string Message)
 {
   // seu codigo
 }

If you need a tutorial I recommend your own asp.net: link

    
09.03.2018 / 13:08