将参数传递给Controller ..但不在URL上 [英] Passing parameters to Controller ..but NOT on the URL

查看:185
本文介绍了将参数传递给Controller ..但不在URL上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将参数传递给控制器​​而不将其放在URL上?

Is there a way to pass a parameter to a controller without putting it on the URL?

例如, http://www.winepassionate.com/p/19/wine-chianti-docg-la-moto

在URL上的值为19.如果您实际上将该值更改为另一个值,则即使页面名称保持不变,页面也会显示不同的记录.

has the value 19 on the URL. If you actually change that value to another, the page displays a different record even it the page name remains the same.

因此,我不想在URL上传递ID,但仍然能够将其传递给Controller. 建议这样做的方法是什么?

So I would like to NOT pass the ID on the URL but still be able to pass that to the Controller. What's the recommended way to do so?

推荐答案

嗯,您有两种选择:

  1. 这是表格吗?如果是这样,那么您只需在提交表单时将一个特定的键值对添加到表单中,然后就可以传递数据.
  2. URL是该资源唯一的吗?即,"Wine-chianti-docg-la-moto"是否作为某处数据库中数字19的唯一表示形式存在?如果是这样,那么您只需在数据库中查找该路由组件即可检索所需的值(或将该逻辑一直推送到数据库中).
  3. 难道不是一个会改变一堆的值吗?您可以在Session中或将在各个页面中保留的Cookie中设置该值,然后将其从相应的集合中提取.
  4. 您要从服务器上的另一个请求重定向到此页面吗?如果是这样,则可以使用TempData存储此临时值.但是,我建议您不要使用这种方法,因为它非常短暂,不是imo的良好做法.
  5. 最后,如果您不希望该URL易于用户编辑,则可以遮盖该URL上的值.使用某种算法对其进行加密,然后在目标页面上对其进行解密.用户不太可能通过在URL中键入其他值来更改ID.
  1. Is this a form post? If so, then you can simply add a specific key value pair to your form when you submit it and then data will be passed along.
  2. Is the URL unique to that resource? i.e. Does "Wine-chianti-docg-la-moto" exist as a unique representation of the number 19 in a database somewhere? If so, then you can simply do a lookup of that route component in your database to retrieve the value you need (or push that logic all the way down to the database).
  3. Is that a value that is not expected to change a bunch? You can set that value in Session or in a cookie that would be persisted across pages and then pull it from the respective collection.
  4. Are you redirecting to this page from another request on your server? If so, then you can use TempData to store this temporary value. However, I would recommend against this approach, as it is very transient and not good practice imo.
  5. Lastly, you can obscure the value on the URL if you dont want it to be easily user editable. Encrypt it with some algorithm, and then decrypt it on the destination page. The user will be unlikely to be able to alter the ID by typing in a different value in the URL.

这篇关于将参数传递给Controller ..但不在URL上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆