简体   繁体   中英

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. 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.

How can i prevent this.

In this scenario, what you really should be doing is return a view rather than 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:

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. 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.

The recommended approach in this case it to use TempData at the controller to set all your server-side data. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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