Pass Array into ASP.NET Core Route Query String

前端 未结 8 1410
既然无缘
既然无缘 2020-12-05 04:28

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&         


        
相关标签:
8条回答
  • 2020-12-05 05:23

    I have found a solution. For example, if you have a query like this:

    http://www.sitename.com/route?arr[]=this&arr[]=that

    You must define in parameter as [FromQuery(Name = "arr[]")]. The name of parameter must include square brackets. As result we can see:

    public void DoSomething([FromQuery(Name = "arr[]")] string[] arr)
    
    0 讨论(0)
  • 2020-12-05 05:23

    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.

    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).

    0 讨论(0)
提交回复
热议问题