How do I hide URL IDs?

1

17/11/2016

I have an application that is building the following URL

www.mysite.com/controller/action/id

However, I need the ID not to be displayed in the URL, but to leave it alive in back-end , because it is a requests screen, and I would not like to leave the id of the request running in the URL.

Each order is tied to a store, and I have a validation so that only the logins of the xpto store access requests from the xpto store, not requests from other stores.

I just need to get rid of the URL ID and make it live on the system, but it's kind of complicated to find a solution. I have read several articles, but nothing enlightening.

Who can help, I thank you.

18/11/2016

Marllon Nasser, I followed your idea and did it as follows:

@this.Hidden("IdPedido").Value(item.Id)

<a class="btn btn-primary btn-xs btn-visualizar" target="_blank" data-toggle="tooltip" title="Visualizar">
<i class="fa fa-eye">< /i>
</a>

$(function() {
  $(".btn-visualizar").click(function() {
    var pedido = $("#IdPedido").val();
    $.post("@(Url.Action(MVC.Painel.Pedidos.Visualizar()))", {
      id: pedido
    })
  });
});

It goes to the controller passing the request id, but in my controller I need to open another view called "requests".

[HttpPost]
public virtual ActionResult Visualizar(int id)
{
    var user = TUser.FindByUserName(this.User.Identity.Name);
    var pedido = TOrder.Load(id);
    var xpto = pedido.Franchise.Name;
    if (user.Franchise == null || user.Franchise.Id == pedido.Franchise.Id)
    {
        //return View(pedido);
        return RedirectToAction("Visualizar", pedido);
    }
    else
    {
        TempData["Error"] = "Pedido não pertence a esta loja";
        return RedirectToAction("Index");
    }
}

But it's not opening. How can I make any suggestions?

    
asked by anonymous 17.11.2016 / 17:54

3 answers

1

The problem is that you are doing this request using a "get" method instead of "post"

Here a good explanation!

    
17.11.2016 / 18:54
1

The best way to do this would be POST. You can leave the id inside the form in an input to type hidden.

    
17.11.2016 / 20:19
1

I made a comment but I decided to put it in response. Just use the protocol POST instead of GET .

Speaking for MVC itself, you can not do the direct modification on your ActionLink . ActionLink itself renders hyperlink .

But if you're using MVC 3+, you can use Ajax.ActionLink() , which would look something like:

@Ajax.ActionLink("Clique Aqui", "Action", "Controller", new { id = SeuId }, new AjaxOptions {HttpMethod = "POST"})

Remembering that for this to work, you need to include a jquery library called jquery.unobtrusive-ajax.min.js . The advantage of this approach is that you can tell which protocol you want.

Or if you prefer, you can make use of it with jQuery:

<a id="teste_post" href="javascript:void(0);">Clique Aqui</a>

$("#teste_post").click(function() {
    $.ajax({
        type: "POST",
        url: '@Url.Action("Action", "Controller")',
        data: { id: SeuId },
        success: function (d) {
            //TODO
        },
        error: function (xhr, textStatus, errorThrown) {
            //TODO
        }
    });
});

Of course, for any of the options presented, your action must be noted as [HttpPost] :

[HttpPost]
public ActionResult Action(int id)
{
   //TODO
}

EDIT

Following your implementation, I suggest that you return a PartialView in the Visualizar .

Your ajax implementation also needs to be treated, going something like this:

$(".btn-visualizar").click(function() {
  var pedido = $("#IdPedido").val();
  $.post("@(Url.Action(MVC.Painel.Pedidos.Visualizar()))", {
    id: pedido
  }).done(function(data) {
    // aqui é o retorno da controller
    var error = '@TempData["Error"]';
    if (error == null || error == "") {
      //sucesso
      $("#aDivOndeVoceVaiExibir").html(data);
    } else {
      //erro
      alert(error);
      location.href = '@Url.Action("Index", "SuaController")';
    }
  });
});

And its controller more or less like this:

[HttpPost]
public virtual ActionResult Visualizar(int id)
{
    var user = TUser.FindByUserName(this.User.Identity.Name);
    var pedido = TOrder.Load(id);
    var xpto = pedido.Franchise.Name;
    if (user.Franchise == null || user.Franchise.Id == pedido.Franchise.Id)
    {
        return PartialView("_Visualizar", pedido);
    }
    TempData["Error"] = "Pedido não pertence a esta loja";
    return null;
}

See also:

17.11.2016 / 19:34