3
I am with a project in Asp.net mvc 4, using Attributerouting to set up the routes, and I’m checking a strange behavior.
When mounting my GET and POST actions in that order:
[GET("editar/{id}")]
public ActionResult Editar(int id)
{
return View();
}
[POST("salvar-edicao")]
public ActionResult Editar(string teste)
{
return View();
}
The address of my form is as follows:
<form action="/editar/1" method="post"> <input type="submit" value="Enviar">
Making sure he doesn’t post to the right place, returning me 404. However, if I reverse the order of the two methods by putting the POST as first, the form address is mounted correctly, but the link to call the get no:
[POST]
public ActionResult Editar(string teste)
{
return View();
}
[GET("editar/{id?}")]
public ActionResult Editar(int id)
{
return View();
}
form:
<form action="/Editar" method="post">
link:
@Html.ActionLink("Teste", "Editar", "Home", new { id = 1 }, new { })
mounted like this
http://localhost/Editar?id=1
instead http://localhost/Editar/1
Can someone help me?
Why don’t you use the same action as GET/POST?
– PauloHDSousa
This was just an example, but maybe it’s a problem because the whole application is modeled this way. How would it be using the same action for GET/POST?
– tparesque
public Actionresult Edit(int id) { if(string.Isnullorempty(id)){ //Edit } Else { //not edit } Return View(); }
– PauloHDSousa
Tries to change the
[POST]
for[HttpPost]
– Diego Zanardo
[POST] is from Attributerouting.Web.Mvc, I can add the [Httppost], but the behavior will be identical.
– tparesque