asp.net-mvc 如何在MVC4中隐藏URL的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14826975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Hide Parameters of URL in MVC4
提问by Gowtham
http://localhost:49397/ChildCare/SponsorChild/83
http://localhost:49397/ChildCare/SponsorChild/83
This is the Link ,which is being generated when i click on action link in table and redirecting to Edit Action, now i want to Hide the number '83' in the URL how can i acheive this,
这是链接,当我单击表中的操作链接并重定向到编辑操作时生成的链接,现在我想隐藏 URL 中的数字“83”,我该如何实现,
i am using VS2010 MVc4 Razor, Sorry for my bad engllish thanks in advance
我正在使用 VS2010 MVc4 Razor,抱歉我的英语不好,提前致谢
采纳答案by andres descalzo
if you work with links, the links send by GETrequest to the server, then the parameters are in the url. Might you have two options:
如果您使用链接,链接通过GET请求发送到服务器,则参数在 url 中。你可能有两个选择:
1 - the parameters would have to be on dataattributes like data-id="83"and then create a form to send data by post, and creating tags inputwith attributes data-x, for example:
1 - 参数必须在data属性上data-id="83",然后创建一个表单以通过 post 发送数据,并创建input带有属性的标签data-x,例如:
<a href="my/url" data-id="83> link </a>
then with javascript you need create the form:
然后使用 javascript 您需要创建表单:
<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()
并使用 JS 表单提交运行事件,如: jQuery('form').submit()
2 - you can encrypt and then decrypt get parameters in the controller: How to encrypt and decrypt data in MVC?
2 - 您可以在控制器中加密然后解密获取参数:如何在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:
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
});
回答by Matas Vaitkevicius
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 RestoreSavedSessionit then takes parameter stores it locally and calls RestoreSavedSessionValidationwhere it reads parameter from Sessionor Cacheor whatever.
您点击RestoreSavedSession它然后将参数存储在本地并调用RestoreSavedSessionValidation它从会话或缓存或其他任何读取参数的位置。
回答by Darren Lim
I uses a preview method store the route data to TempData, and route it to the correct action.
我使用预览方法将路由数据存储到 TempData,并将其路由到正确的操作。
public async Task<ActionResult> Preview(string act, string ctl, string obj)
{
TempData["Data"] = obj;
return RedirectToAction(act, ctl);
}
To use it
使用它
return RedirectToAction("Preview","Controller",new {act="action",ctl="controller",obj=JsonConvet.SerializeObject(obj)});
After routing
路由后
var x=JsonConvert.DeserializeObject<T>(TempData["Data"].ToString());

