如何获得查询字符串变量在MVC 4的请求? [英] How to get query string variables in MVC 4's Request?

查看:142
本文介绍了如何获得查询字符串变量在MVC 4的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里guffawed上与网页API品种我的第一个MVC 4项目。

I'm guffawed here working on my first MVC 4 project with the Web Api variety.

在MVC 3,我可以得到一个查询字符串参数等一样:

In MVC 3 I could get a query string parameter like such:

var unicornName = Request.Query["unicornName"];

但在MVC 4,它看起来像请求从的Htt prequestBase 的Htt prequestMessage 和查询参数是没有更多的。所以,嗯,好吧,我现在该如何得到它们。我在网上找到了几个例子,但他们是荒谬的。

But in MVC 4, it looks like the Request went from a HttpRequestBase to a HttpRequestMessage and the Query parameter is no more. So, hmm, okay, how do I get them now. I found a couple examples on the web but they are absurd.

这家伙被建议分裂RequestUri的查询字符串与&并找到你的参数及对。 而这个例子显示呼吁新的请求 GetQueryNameValuePairs 方法对象返回键值对的列表,然后做一些LINQ找到你的key和value。它不能真正成为这个倒着拿东西那么简单。请告诉我,我失去了一些东西!

This fellow recommends splitting the RequestUri's query string by "&" and finding your param and pair. And this example shows calling a GetQueryNameValuePairs method on the new request object which returns a list of key value pairs , then doing some linq to find your key and value. It can't really be this backwards to get something so simple. Please tell me I'm missing something!

注意:我能理解这是怎么回事的模型绑定,我应该通过诉讼的方法PARAMS被引进的查询字符串参数的方式,但还是有当查询字符串变量需要被人拔光倍(容易吗?)从请求,例如在一个过滤器。

Note: I can understand it's going the way of model binding and I should be bringing in query string parameters via the action's method params, but there are still times when query string variables need to be plucked (easily?) from the Request, such as in a Filter.

推荐答案

我想这可能是你在找什么,

I think this may be what you are looking for,

  var queryValues = Request.RequestUri.ParseQueryString();

http://stackoverflow.com/a/11729619/6819

这篇关于如何获得查询字符串变量在MVC 4的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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