Help with ASP MVC routes (3 routes for the same action)

1

I have a controler:

public class imoveisController : Controller
{
    public ActionResult Index(int idEstado = 0, int idCidade = 0, int[] idRegiao = null)
    {
        string IdEstado;
        string IdCidade;
        int[] IdRegiao;

        #LeOuGravaCookie

        #region MontaViewBags

        #region RetornaBairros

        #region RetornaImoveisDessesBairros

        return View(ImoveisRetornados);
    }
}

In the view there is a form where the user can select 1 state and 1 city and a set of neighborhoods. And when submitted returns to the Action Index that instead of reading this data a cookie will receive the form, do the search and return the real estate. Running 100% right.

My problem is that on return it displays for p user the following url:

  

Myth / Real Estate

And I need to return depending on what the user selects on the form If he just reported the status:

  

mysite / real estate / {statusInformed}

If he also selected a city:

  

mysite / real estate / {statusInformed} / {CityInformed}

Could anyone help me do this?

    
asked by anonymous 14.12.2017 / 20:38

1 answer

1

You can use the following strategy:

[RoutePrefix("home")]
public class HomeController : Controller
{
    [HttpGet, Route("inicio")]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost, Route("inicio/{uf?}/{municipio?}")]
    public ActionResult Index(string uf, string municipio)
    {
        return RedirectToAction("Busca", new { uf = uf, municipio = municipio });
    }

    [Route("busca/{uf?}/{municipio?}")]
    public ActionResult Busca(string uf, string municipio)
    {
        return View("Index");
    }    
}

Form with fields:

@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("Index", "Home"))
{
    <input name="uf" id="uf" type="text" />
    <input name="municipio" id="municipio" type="text" />
    <input type="submit" value="BUSCAR" />
}

//Lembrando que é necessário configurar os MapMvcAttributeRoutes no arquivo RouteConfig.cs
routes.MapMvcAttributeRoutes();

When you submit the report it will send to Action Search with the new URL.

    
15.12.2017 / 18:55