简体   繁体   中英

ASP.NET MVC parsing variables wihout URL query string

Heading

I want to pass some variables from one page to another but I don't want them to appear in the URL. When I use a form to post the an ActionResult it works fine. However, when I do this

return RedirectToAction("Trackers",new{page = 1, orderby=desc});

I get the URL:

http://examplesite.com/Trackers?page=1&orderby=desc

Is there any way I can get the following URL instead but still pass the page and orderby variables "behind the scenes"?

http://examplesite.com/Trackers

Thanks TheLorax

I'm not sure it's a good idea to do what you're suggesting, however, you might be able to use the TempData (same place as the ViewData) to save your information before the redirect. I believe that after your next request, anything in the temp data is cleared out.

You could pass a Model object containing the page number and the orderby instead of an anonymous object, and have your view inherit the model class of the object you are passing. Your model object with the page number and orderby then becomes available to the view, without employing a query string

This is not, however, the preferred method of doing this. Your model objects should be reserved for their intended purpose, which is passing actual data to your view.

When you use RedirectToAction method the browser will recieve an http redirect response from the server and then will do a GET request to the new URL.
I think there is no way to make the browser make a POST request when redirecting.

Also it is better to pass these variables through the query string in order for the user to be able to bookmark the page.

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