I am trying to access query string parameters in my ASP.NET MVC6 applications. But it seems unlike MVC5 and web forms, QueryString doesn't have any indexer and I can't say something like:
我正在尝试访问我的ASP中的查询字符串参数。净MVC6应用程序。但它似乎不像MVC5和web表单,QueryString没有索引器,我不能说:
string s = Request.QueryString["key1"] //gives error
So, my question is - how do I access query string parameters in MVC6?
我的问题是——如何在MVC6中访问查询字符串参数?
Surprisingly Request.Forms
collection works as expected (as in MVC5 or web forms).
令人惊讶的是请求。表单集合按预期工作(如MVC5或web表单)。
Thank you.
谢谢你!
2 个解决方案
#1
29
Getting query with an indexer is supported.
支持使用索引器获取查询。
See MVC code test here - https://github.com/aspnet/Mvc/blob/e0b8532735997c439e11fff68dd342d5af59f05f/test/WebSites/ControllersFromServicesClassLibrary/QueryValueService.cs
请参阅下面的MVC代码测试——https://github.com/aspnet/mvc/blob/e0b85327327357c439e11fff68342d5af59f/test/websites/controllersfromservicesclasslibrary/queryvalueservice.cs
context.Request.Query["value"];
Also note that in MVC 6 you can model bind directly from query by using the [FromQuery] attribute.
还要注意,在MVC 6中,您可以使用[FromQuery]属性直接从查询中建模绑定。
public IActionResult ActionMethod([FromQuery]string key1)
{
...
}
#2
17
So, my question is - how do I access query string parameters in MVC6?
我的问题是——如何在MVC6中访问查询字符串参数?
You can use Request.Query
which is new addition in ASPNET 5.
您可以使用请求。查询是在ASPNET 5中新增的。
var queryStrings = Request.Query;
The URL I am going to try was - http://localhost:12048/Home/Index?p=123&q=456
And you can get All Keys using -
我要尝试的URL是- http://localhost:12048/Home/Index?p=123&q=456,你可以使用-获取所有的键
queryStrings.Keys
And then you can get the values by iterating keys -
然后你可以通过迭代键-来得到值
var qsList = new List<string>();
foreach(var key in queryStrings.Keys)
{
qsList.Add(queryStrings[key]);
}
#1
29
Getting query with an indexer is supported.
支持使用索引器获取查询。
See MVC code test here - https://github.com/aspnet/Mvc/blob/e0b8532735997c439e11fff68dd342d5af59f05f/test/WebSites/ControllersFromServicesClassLibrary/QueryValueService.cs
请参阅下面的MVC代码测试——https://github.com/aspnet/mvc/blob/e0b85327327357c439e11fff68342d5af59f/test/websites/controllersfromservicesclasslibrary/queryvalueservice.cs
context.Request.Query["value"];
Also note that in MVC 6 you can model bind directly from query by using the [FromQuery] attribute.
还要注意,在MVC 6中,您可以使用[FromQuery]属性直接从查询中建模绑定。
public IActionResult ActionMethod([FromQuery]string key1)
{
...
}
#2
17
So, my question is - how do I access query string parameters in MVC6?
我的问题是——如何在MVC6中访问查询字符串参数?
You can use Request.Query
which is new addition in ASPNET 5.
您可以使用请求。查询是在ASPNET 5中新增的。
var queryStrings = Request.Query;
The URL I am going to try was - http://localhost:12048/Home/Index?p=123&q=456
And you can get All Keys using -
我要尝试的URL是- http://localhost:12048/Home/Index?p=123&q=456,你可以使用-获取所有的键
queryStrings.Keys
And then you can get the values by iterating keys -
然后你可以通过迭代键-来得到值
var qsList = new List<string>();
foreach(var key in queryStrings.Keys)
{
qsList.Add(queryStrings[key]);
}