Hide URL parameters with routes and mvc 5

3

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 the page loads, my url looks like this.

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

The 3/3 are the respective parameters passed in the route. Can you hide them in the URL?

I have a foreach (in the home) that mounts 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 into the database and takes the HTML corresponding to that parameter. If I could pass the parameter, other than the URL, I would refer to Route, to display only the name, since the HTML will be shown by the parameter passed in another way.

    
asked by anonymous 21.03.2014 / 16:03

1 answer

1

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

Now the problem is how to do a POST from a link ... the way I know it 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 within that form and set the value of them 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>
    
21.03.2014 / 18:59