URL of route being incorrectly mounted

Asked

Viewed 241 times

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?

  • 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?

  • public Actionresult Edit(int id) { if(string.Isnullorempty(id)){ //Edit } Else { //not edit } Return View(); }

  • Tries to change the [POST] for [HttpPost]

  • [POST] is from Attributerouting.Web.Mvc, I can add the [Httppost], but the behavior will be identical.

3 answers

1


Apparently for the form to be posted correctly using attribute, the methods must have identical routes.

[POST("editar/{id}")]
public ActionResult Editar(Pessoa teste)
{
    return View();
}

[GET("editar/{id}")]
public ActionResult Editar(int id)
{
    return View();
}

This way the links and forms are assembled and respond correctly.

0

Controller:

public ActionResult Editar(int id)
{
    return View();
}

[HttpPost]
public ActionResult Editar(string teste)
{
    return View();
}

In his View:

@using (Html.BeginForm()) {
   <input type="text" id="teste" />
   <input type="submit" value="Enviar" />
}

And to generate the link:

@Html.ActionLink("Teste", "Editar", "Home", new { id = 1 }, null)
  • Hello, Diego. That way he keeps mounting the form the way I showed above, making the post action not located in the controller. <form action="/edit/1" method="post"><input id="test" name="test" type="text" value="" " /><input type="Submit" value="Send" /> </form>

  • @like, I never messed with the AttributeRouting, but from what I understand he forces his way into Get, if you withdraw the [GET("editar/{id}")], will work normally.

0

I believe the routing is being generated wrong because you are using the two actions with the same name, just changing the data type, however when generating the routing the data type received is not observed to generate the route.

I believe that if you do the following will work:

[GET("editar/{id}")]
public ActionResult Editar(int id)
{
    return View();
}

[POST("salvar-edicao")]
public ActionResult Editar(int id, string teste)
{
    return View();
}
  • Hello, @Richard Dias. Well noted, I did the test here, but the form continues to be mounted the same way... Is it an attributeRouting bug? If this same native approach of Asp.net mvc would work correctly only with [Httpget] and [Httppost], however, customizing routes with this framework is a premise of the project.

Browser other questions tagged

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