将数组传递到 ASP.NET Core 路由查询字符串 [英] Pass Array into ASP.NET Core Route Query String
问题描述
我想做this,但我也希望能够将数组传入查询字符串.我试过这样的事情:
I want to do this, but I want to also be able to pass in arrays into the query string. I've tried things like:
http://www.sitename.com/route?arr[]=this&arr[]=that
http://www.sitename.com/route?arr[]=this&that
http://www.sitename.com/route?arr[0]=this&arr[1]=that
http://www.sitename.com/route?arr0=this&arr1=that
http://www.sitename.com/route?arr=this&arr=that
我在 C# 代码中的路径如下所示:
And my route in the C# code looks like this:
[Route("route")]
[HttpGet]
public void DoSomething(string[] values)
{
// ...
}
但在所有这些情况下,当它到达 C# 代码时,值始终为空.我需要什么查询字符串才能传递字符串数组?
But in all of these cases, values is always null when it gets to the C# code. What do I need my query string to be to pass an array of strings?
推荐答案
最后,我只是传入一个单独的分隔字符串,然后在服务器端使用 string.Split 进行分隔.不是最漂亮的解决方案,但它有效.在有人想出更好的答案之前,这就是我所得到的.我应该重申,我使用的是 .NET Core,并且这些查询字符串是特定于框架的.
In the end, I just passed in a single delimited string, then used string.Split to separate on the server side. Not the prettiest solution, but it works. Until someone comes up with a better answer, this is all I got. I should reiterate that I'm using .NET Core, and these query strings are framework specific.
更新:这种方法的一个(有争议的)好处是您可以将值一起传递(例如 arr=value1,value2),而不是重复键(arr=value1&arr=value2).
Update: An (arguable) benefit of this approach is that you pass the values together (e.g. arr=value1,value2) instead of repeating the key (arr=value1&arr=value2).
这篇关于将数组传递到 ASP.NET Core 路由查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!