ASP.NET MVC中如何获取所有活动参数(二)

发布于 2024-08-28 17:53:51 字数 1338 浏览 8 评论 0原文

我想知道是否有一种方法可以创建 ActionLink 或类似的方法,只更改实际查询的几个参数,并保持所有其他参数不变。例如,如果我所在的 URL 类似于 http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True 我只想更改 PageOrderBy 参数,并保持所有其他参数相同,即使是那些我还不知道的参数(例如当我想添加 Search 参数或也有类似的东西)。

我当前操作的标题如下所示:

public ActionResult Index(int? Page, string OrderBy, bool? Desc)

我只对此控制器“吃”的值感兴趣。然而,我希望当我扩展此操作(例如使用 string Search 参数)时,链接的工作方式应与以前相同。

这是我已经做的:

  1. 创建一个新的 RouteValueDictionary 并用 RouteData.Values 中的所有内容填充它
    • 问题:这仅填充路由中使用的参数,因此控制器的所有其他可选参数(例如 Page)都会丢失
  2. 添加来自 HttpContext.Request.QueryString 的所有内容 到上一个字典
    • 这是我目前正在使用的
    • 问题:它可能有一些控制器没有要求的垃圾内容,并且如果使用 POST 加载页面则无法工作。您也没有任何 ModelBindings(但这不是什么大问题,因为我们无论如何都会重新发送所有内容)
  3. 使用 HttpContext.Request.Params
    • 问题:这包含太多垃圾数据,恕我直言,不应将其添加到传递给 ActionLinkRouteValueDictionary

所以问题是:

  1. 是否有 < code>RVD 包含传递给控制器​​并由其使用的所有数据?
  2. 这个解决方案好吗,或者有什么我没有想到的警告(主要是在更改一些查询参数同时保持其他参数不变的情况下)?
  3. 有没有办法从 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:

  1. Create a new RouteValueDictionary and fill it with everything from RouteData.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
  2. 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)
  3. Use HttpContext.Request.Params
    • Problem: this has too much junk data which imho one shouldn't add to a RouteValueDictionary that is passed to an ActionLink

So the questions:

  1. Is there an RVD that has all the data that was passed to the Controller and was used by it?
  2. 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)?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-09-04 17:53:51

查看RouteData.DataTokens

RouteData.DataTokens @ MSDN 文档 :

获取传递给路由处理程序但在 ASP.NET 路由确定路由是否与请求匹配时不使用的自定义值的集合。

HTH,
查尔斯

Have a look in RouteData.DataTokens.

RouteData.DataTokens @ MSDN documentation:

Gets a collection of custom values that are passed to the route handler but are not used when ASP.NET routing determines whether the route matches a request.

HTHs,
Charles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文