How to put two action with HTTPPOST

Asked

Viewed 578 times

-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

  • That’s right, but that’s not called controller, it’s called action (action). What is your doubt?

  • If I put this it gives error only works if one is Post and the other get

  • The answer was clear?

  • Not exactly, not quite sure what to do to fix

  • 1

    You cannot have two actions with the same name and HttpPost, you need to change the name, or one should be HttpGet

  • @Amadeuantunes Change name, young man.

Show 1 more comment

3 answers

1

See what the error says (highlight made 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’t have two shares with the same name this way.

Because when you call the application will not know whether to call the first action with the last parameter as null or is called the second action.

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

1

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

class Index is only to load page content 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 recommend the Asp.net itself : https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller

0


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

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

In the functions you must 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
}

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

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

[Route("receber/{id:string}")]

Browser other questions tagged

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