asp.net-mvc-5 – Hide parameters in URL with routes and mvc 5

Question:

I have a route like this:

routes.MapRoute(
    name: "RotaConteudo",
    url: "Conteudo/{name}/{Parametro}/{tipo}",
    defaults: new { controller = "Conteudo", action = "Conteudo", name = "", Parametro = "", tipo = "" }
);

When you go up the page, my url looks like this.

..../Conteudo/argentina/3/3

The 3/3 are the respective parameters passed in the route. Is there a way to hide them in the URL?

I have a foreach (in the home) that assembles my URL, like this:

foreach (var item3 in item2.subconsulta2)
{
    if (item3.Id_SubCategoria2 != null)
        str = str + "<li><a href=\"/Conteudo/" + item3.Id_SubCategoria2 + "/3/" + retira_acentos(item3.SubCategoria2).ToLower().Replace(" ", "-") + "\" title=\"\">" + item3.SubCategoria2 + "</a></li>";
    else
        str = str + "<li><a>" + item3.SubCategoria2 + "</a></li>";
}

It sends these parameters to a method I have in a controller and it goes to the database and gets the HTML corresponding to that parameter. If I could pass the parameter, other than the URL, I would redo the Route, to display just the name, as the HTML will be shown by the parameter passed in another way.

Answer:

To send data to the server other than the URL, the only way I know of is via POST .

Now the problem is how to make a POST from a link… the way I know is you create a form on the page and submit it using javascript on the click of the link, and to hide some of the parameters, just put input s inside this form and set their value before submitting via script.

Functional example: using jquery

Routes:

routes.MapRoute(
    name: "RotaConteudo",
    url: "Home/{name}",
    defaults: new { controller = "Home", action = "Conteudo", name = "" }
);

Javascript:

function linkPost(e, params) {
    e.preventDefault();

    var $form = $("<form action='' method='POST'></form>");

    for (var key in params)
        $("<input type='hidden'/>").attr("name", key).val(params[key]).appendTo($form);

    $form
        .attr('action', $(this).attr('href'))
        .submit();

    return false;
}

CsHtml:

<a href="@Url.Action("Conteudo", new { name = "agt" })"
   id="open-popup"
   onclick="javascript:return linkPost.apply(this, [event, { parametro: 3, tipo: 3}]);"
>agt 3, 3</a>
Scroll to Top