简体   繁体   English

Asp.net MVC RedirectToAction在URL中显示参数

[英]Asp.net MVC RedirectToAction Show Parameter in URL

I need to Redirect to Action with a model object from another action method. 我需要使用另一个动作方法中的模型对象重定向到动作。 It is ok but when i do this, i can see all the parameters in the URL address bar. 没关系,但是当我这样做时,我可以看到URL地址栏中的所有参数。 Since it is about payment, it is not ok for me ? 既然是付款,对我来说不合适吗?

I can do this basicly passing ID but my model is viewmodel and there is no Key. 我可以基本上传递ID,但我的模型是viewmodel,没有Key。

How can i prevent this. 我怎么能阻止这个。

In this scenario, what you really should be doing is return a view rather than redirect . 在这种情况下,您真正​​应该做的是返回视图而不是redirect

Like: 喜欢:

return View(viewModel);

But if you really prefer to do a redirect, you can place the ViewModel in the TempData, and then redirect to the action: 但是,如果您真的更喜欢进行重定向,可以将ViewModel放在TempData中,然后重定向到该操作:

TempData["MyViewModelFromRedirect"] = viewModel;

And in your redirected action: 在您的重定向操作中:

var ViewModel = (MyViewModel)TempData["MyViewModelFromRedirect"];

Redirect result will return the HTTP redirect result (302) to the browser, with the whole parameters in the url. 重定向结果将HTTP重定向结果(302)返回给浏览器,其中包含url中的所有参数。 If you're passing model properties in the routevalues, they will be serialised to strings. 如果您在路由值中传递模型属性,它们将被序列化为字符串。

So, as you said the browser (at client side) will see all those parameters, and the browser will issue another GET request to the new url. 因此,正如您所说,浏览器(在客户端)将看到所有这些参数,浏览器将向新网址发出另一个GET请求。

The recommended approach in this case it to use TempData at the controller to set all your server-side data. 在这种情况下,推荐的方法是在控制器上使用TempData来设置所有服务器端数据。 Then redirect to the new action. 然后重定向到新操作。

TempData["mymodel"] = myModel;
return Redirect(Url.Action("newaction", "newcontroller"));

And in the new Action, you can just retrieve your model from TempData 在新的Action中,您可以从TempData中检索模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM