简体   繁体   English

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

[英]How to get all active parameters in ASP.NET MVC (2)

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. 我想知道是否有一种方法可以创建一个ActionLink或类似的方法,该方法仅更改实际查询的几个参数,并使所有其他参数保持不变。 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). 例如,如果我使用的网址为http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True我只想更改PageOrderBy参数,并保持所有其他参数不变,即使那些我还不知道(例如,当我想添加Search参数或类似的东西时)。

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. 但是,我希望当我扩展此操作(例如,使用string Search参数)时,链接的工作方式应与以前相同。

Here is what I did already: 这是我已经做过的:

  1. Create a new RouteValueDictionary and fill it with everything from RouteData.Values 创建一个新的RouteValueDictionary并用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 问题:这仅填充了路由中使用的参数,因此控制器的所有其他可选参数(如Page )都丢失了
  2. Add everything from HttpContext.Request.QueryString to the previous dictionary HttpContext.Request.QueryString所有内容添加到上一词典
    • 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. 问题:可能有一些垃圾内容,Controller并没有要求,如果使用POST加载了页面则无法正常工作。 You also don't have any ModelBindings (but this isn't much of a problem, because we are re-sending everything anyway) 您也没有任何ModelBindings(但这不是什么大问题,因为无论如何我们都会重新发送所有内容)
  3. Use HttpContext.Request.Params 使用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 问题:这有太多垃圾数据,恕我直言,不应将这些数据添加到传递给ActionLinkRouteValueDictionary

So the questions: 所以问题是:

  1. Is there an RVD that has all the data that was passed to the Controller and was used by it? 是否有一个RVD ,具有所有传递到控制器并由其使用的数据?
  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? 有没有办法从Params对象中过滤掉“垃圾”数据?

EDIT: Checked the RouteData.DataTokens variable, but it's usually empty, and doesn't contain everything I need. 编辑:检查RouteData.DataTokens变量,但是它通常为空,并且不包含我需要的所有内容。 It seems to only contain parameters that are needed for the routing somewhere, but not all of the parameters. 它似乎只包含某处路由所需的参数,而不是所有参数。

Have a look in RouteData.DataTokens . 看看RouteData.DataTokens

RouteData.DataTokens @ MSDN documentation : RouteData.DataTokens @ MSDN文档

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

HTHs, HTH,
Charles 查尔斯

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

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