Pass Array into ASP.NET Core Route Query String
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).
How to pass an array of objects through a query string and read them in asp.net Controller
You have to add from [FromQuery]
public IActionResult GetInvoiceNum(
[FromQuery] string xDateTime,
[FromQuery] string BuyerName,
[FromQuery] double TotalBillAmount,
[FromQuery] InvItem[] items
)
and convert fields to properties by adding getters/setters
public class InvItem
{
public string ItemCode { get; set; }
public double Quantity { get; set; }
public double SaleValue { get; set; }
}
ASP.NET Core Web API : route by query parameter
You are trying to differentiate API calls using query params
. this is not the way to do this. if you want to separate the calls you should probably use path params
instead.
Read more about Routing in ASP.NET Core - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0
pass a json object in query string for a web api?
The first way, you can pass query string like below:https://localhost:portNumber/WeatherForecast?fundcodes=aaa&fundcodes=bbb&fromDate=2021-04-01&toDate=2021-05-01
.
Model:
public class TestModel
{
public string[] FundCodes { get; set; }
public string FromDate { get; set; }
public string ToDate { get; set; }
}
Controller:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public IActionResult Get([FromQuery]TestModel model)
{
return Ok(model);
}
}
The result you will get:
The second way, you can pass query string in browser like below:https://localhost:portNumber/WeatherForecast?json={"FundCodes":["0DFOY"],"FromDate":"2021-04-01","ToDate":"2021-05-01"}
. The browser will dynamic encode this url and pass to backend.
If you want to send encoded url manually, the url should be: https://localhost:portNumber/WeatherForecast?json={%22FundCodes%22:[%220DFOY%22],%22FromDate%22:%222021-04-01%22,%22ToDate%22:%222021-05-01%22}
or https://localhost:portNumber/WeatherForecast?json=%7B%22FundCodes%22%3A%5B%220DFOY%22%5D%2C%22FromDate%22%3A%222021-04-01%22%2C%22ToDate%22%3A%222021-05-01%22%7D
.
Controller:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public IActionResult Get([FromQuery]string json)
{
var model = System.Text.Json.JsonSerializer.Deserialize<TestModel>(json);
return Ok(model);
}
}
how we can design .net core api controller to accept an array value of strings
You can take advantage of the FromQuery
attribute here, specifying the Name
property as str[]
:
public IActionResult Values([FromQuery(Name = "str[]")] List<string> strValues)
{
...
}
If you also want to strip out the "
s for each value, you can use a Select
. Here's a an example:
public IActionResult Values([FromQuery(Name = "str[]")] List<string> strValues)
{
var strValuesWithoutQuotes = strValues.Select(x => x.Trim('"'));
...
}
Related Topics
How to Format a Number in C# With Commas and Decimals
Fastest Way to Find Strings in a File
How to Set Datetimepicker to Month and Year Only Format
Error Reading Jobject from Jsonreader. Current Jsonreader Item Is Not an Object: Startarray. Path
Multidimensional Array from a Txt File
Restsharp Post Request - Body With X-Www-Form-Urlencoded Values
How to Format a String as a Telephone Number in C#
Use Linq to Collaps Multiply Collections into One Collection
Formatting Date in Linq-To-Entities Query Causes Exception
Get All Children to One List - Recursive C#
Regex to Remove All Special Characters from String
How to Check for a Valid Base64 Encoded String
Controlling Datetime Parameter Formatting in Webapi 2
How to Convert Json Date to .Net Datetime During Deserialization
How to Correctly Write Parallel.For With Async Methods
How to Test If a Instance of a Class Is a Specific Generic Type