How to Hide Parameters of URL in MVC4
if you work with links, the links send by GET
request to the server, then the parameters are in the url. Might you have two options:
1 - the parameters would have to be on data
attributes like data-id="83"
and then create a form to send data by post, and creating tags input
with attributes data-x
, for example:
<a href="my/url" data-id="83> link </a>
then with javascript you need create the form:
<form method="POST" action="my/url">
<input value="83 name="id" type="hidden" />
</form>
and run the event with JS form submit like: jQuery('form').submit()
2 - you can encrypt and then decrypt get parameters in the controller: How to encrypt and decrypt data in MVC?
Edit
Example for point one:
Html:
<div id="container-generic-form" style="display:none;">
<form action="" method="POST"></form>
</div>
<a href="my/url" data-id="83" data-other="blue" class="link-method-post">my link</a>
JS:
$(function() { // document ready
var controlAnchorClickPost = function(event) {
event.preventDefault(); // the default action of the event will not be triggered
var data = $(this).data(),
form = $('#container-generic-form').find('form');
for(var i in data) {
var input = $('<input />', {
type: 'hidden',
name: i
}).val(data[i]);
input.appendTo(form);
}
form.submit();
};
$('a.link-method-post').on('click', controlAnchorClickPost); //jquery 1.7
});
We use Two pages like that to hide the variable
public ActionResult RestoreSavedSession(string id)
{
Session["RestoreSavedSession"] = id;
return RedirectToAction("RestoreSavedSessionValidation");
}
public ActionResult RestoreSavedSessionValidation()
{
return View("RestoreSavedSessionValidation");
}
You hit RestoreSavedSession
it then takes parameter stores it locally and calls RestoreSavedSessionValidation
where it reads parameter from Session or Cache or whatever.