Hide parameters in the URL with routes and mvc 5

Asked

Viewed 1,832 times

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

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

The 3/3 are the respective parameters passed on the route. There is a way to hide them in the URL?

I have a foreach(at home) that mounts my URL, so:

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 that I have in a controller and it goes in the database and takes the HTML corresponding to this parameter. If I could pass the parameter, other than the URL, I would refocus the Route, to display only the name, since the HTML will be shown by the parameter otherwise passed.

  • I have a foreach(at home) that mounts my URL, like foreach (var item3 in item2.subconsulta2) { if (item3.Id_subcategoria2 != null) str = str + "<li><a href="/Content/" + item3.Id_subcategoria2 + "/3/" + remove_accents(item3.Subcategory2). Tolower(). Replace(" ", "-") + "" title="">" + item3.Subcategory2 + "</a></li>"; Else str = str + "<li><a>" + item3.Subcategory2 + "</a></li>"; }

  • Edit your question and add this information that is in comment, right after you have done this, delete the comment.

  • @pnet: Dude, didn’t you have 160 points? Now you only have 6???

  • So, I don’t know what happened. I always posted directly, but this last time, asked me to create a password. I created and asked to create a login. I did it and it worked. Really the site is still a bit confusing. I need to take a tour and understand all this.

  • @pnet: I saw it there in the user list... you ended up creating another user with the same name. Ask there on meta.pt.stackoverflow.com how to solve this problem.

  • @pnet See if it helps ==> http://erraticdev.blogspot.com.br/2011/03/removaing-route-values-from-linksurls-in.html

  • @pnet Have you solved your question? Please give some feedback on this question. ;)

Show 2 more comments

1 answer

1

To send data to the server without the URL, the only way I know 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 at the click of the link, and to hide some of the parameters, just put inputs inside that form and define the value of the same 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>

Browser other questions tagged

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