ASP.NET MVC中如何获取所有活动参数(二)
我想知道是否有一种方法可以创建 ActionLink 或类似的方法,只更改实际查询的几个参数,并保持所有其他参数不变。例如,如果我所在的 URL 类似于 http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True
我只想更改 Page
或 OrderBy
参数,并保持所有其他参数相同,即使是那些我还不知道的参数(例如当我想添加 Search
参数或也有类似的东西)。
我当前操作的标题如下所示:
public ActionResult Index(int? Page, string OrderBy, bool? Desc)
我只对此控制器“吃”的值感兴趣。然而,我希望当我扩展此操作(例如使用 string Search
参数)时,链接的工作方式应与以前相同。
这是我已经做的:
- 创建一个新的
RouteValueDictionary
并用RouteData.Values
中的所有内容填充它- 问题:这仅填充路由中使用的参数,因此控制器的所有其他可选参数(例如
Page
)都会丢失
- 问题:这仅填充路由中使用的参数,因此控制器的所有其他可选参数(例如
- 添加来自
HttpContext.Request.QueryString 的所有内容
到上一个字典- 这是我目前正在使用的
- 问题:它可能有一些控制器没有要求的垃圾内容,并且如果使用 POST 加载页面则无法工作。您也没有任何 ModelBindings(但这不是什么大问题,因为我们无论如何都会重新发送所有内容)
- 使用
HttpContext.Request.Params
- 问题:这包含太多垃圾数据,恕我直言,不应将其添加到传递给
ActionLink
的RouteValueDictionary
- 问题:这包含太多垃圾数据,恕我直言,不应将其添加到传递给
所以问题是:
- 是否有 < code>RVD 包含传递给控制器并由其使用的所有数据?
- 这个解决方案好吗,或者有什么我没有想到的警告(主要是在更改一些查询参数同时保持其他参数不变的情况下)?
- 有没有办法从
Params
对象中过滤掉“垃圾”数据?
编辑:检查了 RouteData.DataTokens 变量,但它通常是空的,并且不包含我需要的所有内容。它似乎只包含某处路由所需的参数,但不是所有参数。
I was wondering whether there is a way to create an ActionLink
or similar, that changes only a few parameters of the actual query, and keeps all the other parameters intact. For example if I'm on an URL like http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True
I want to change only the Page
, or OrderBy
parameter and keep all other parameters the same, even those I don't yet know of (like when I want to add a Search
parameter or something similar too).
The header of my current action looks like this:
public ActionResult Index(int? Page, string OrderBy, bool? Desc)
and I'm only interested in the values that this controller "eats". I want however that when I extend this action (for example with a string Search
parameter) the links should work the same way as before.
Here is what I did already:
- Create a new
RouteValueDictionary
and fill it with everything fromRouteData.Values
- Problem: This only fills the parameters that are used in the Routing, so all other optional parameters (like
Page
) to the controller are lost
- Problem: This only fills the parameters that are used in the Routing, so all other optional parameters (like
- Add everything from
HttpContext.Request.QueryString
to the previous dictionary- This is what I am currently using
- Problem: It might have some junk stuff, that the Controller didn`t ask for, and it doesn't work if the page was loaded using POST. You also don't have any ModelBindings (but this isn't much of a problem, because we are re-sending everything anyway)
- Use
HttpContext.Request.Params
- Problem: this has too much junk data which imho one shouldn't add to a
RouteValueDictionary
that is passed to anActionLink
- Problem: this has too much junk data which imho one shouldn't add to a
So the questions:
- Is there an
RVD
that has all the data that was passed to the Controller and was used by it? - Is this solution good, or are there any caveats I didn't think about (mainly in the context of changing a few query parameters while keeping the others intact)?
- Is there a way to filter out the "junk" data from the
Params
object?
EDIT: Checked the RouteData.DataTokens
variable, but it's usually empty, and doesn't contain everything I need. It seems to only contain parameters that are needed for the routing somewhere, but not all of the parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
RouteData.DataTokens
。RouteData.DataTokens @ MSDN 文档 :
HTH,
查尔斯
Have a look in
RouteData.DataTokens
.RouteData.DataTokens @ MSDN documentation:
HTHs,
Charles