简体   繁体   English

ASP.NET MVC解析变量,没有URL查询字符串

[英]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. 我想将一些变量从一页传递到另一页,但我不希望它们出现在URL中。 When I use a form to post the an ActionResult it works fine. 当我使用表单发布ActionResult时,它可以正常工作。 However, when I do this 但是,当我这样做时

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

I get the URL: 我得到的URL:

http://examplesite.com/Trackers?page=1&orderby=desc 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"? 有什么办法可以代替我获取以下URL,但仍然在“幕后”传递页面和orderby变量?

http://examplesite.com/Trackers http://examplesite.com/Trackers

Thanks TheLorax 感谢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. 我不确定执行建议的操作是否是一个好主意,但是,您可以在重定向之前使用TempData(与ViewData相同的位置)保存信息。 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. 您可以传递一个包含页码和orderby的Model对象,而不是一个匿名对象,并让您的视图继承所传递的对象的模型类。 Your model object with the page number and orderby then becomes available to the view, without employing a query string 然后,具有页码和orderby的模型对象可用于视图,而无需使用查询字符串

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. 当您使用RedirectToAction方法时,浏览器将从服务器接收到http重定向响应,然后将对新URL进行GET请求。
I think there is no way to make the browser make a POST request when redirecting. 我认为没有办法使浏览器在重定向时发出POST请求。

Also it is better to pass these variables through the query string in order for the user to be able to bookmark the page. 同样,最好通过查询字符串传递这些变量,以使用户能够为页面添加书签。

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

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