0

Hi i need to post multiple values to the database at a single request . enter image description here

Here i need to pass multiple storage address and read those values as json in c# . I had tried following code

var request = HttpContext.Current.Request;

string[] StorageAddress = request.Params["StorageAddress"];

but i am not getting response as i required how to pass multiple values through postman url params and read it in code.

UPDATE

enter image description here

chethu
  • 386
  • 1
  • 3
  • 26
  • What framework are you using to handle this request? e.g. API controller. You might want to look at setting up the action on the controller to take the query parameters into part of the route as an input to the action. – Joe_DM Sep 05 '20 at 08:07
  • If your are Web API, decorate your method parameter like this [FromQuery] string[] StorageAddress, this will do the trick. – Purushothaman Sep 05 '20 at 08:26
  • @Joe_DM using C# – chethu Sep 05 '20 at 08:40

2 Answers2

4

Article/Reference: Arrays in query params

Retrieve Option 1

var request = HttpContext.Current.Request;
string[] StorageAddress = request.Params["StorageAddress"].Split(',');

Retrieve Option 2

public IHttpActionResult Get([FromUri] string[] StorageAddress )
{
    ....
}

UPDATE with PostMan passing JSON
enter image description here

You can use Retrieve Option 3

public IHttpActionResult Get(string[] StorageAddress )
{
    ....
}

Retrieve Option 4 with Newtonsoft

var json = await Request.Content.ReadAsStringAsync();
var storageAddress = JsonConvert.DeserializeObject<string[]>(json);

Retrieve Option 5 for mix of json data and files. For postman passing you refer to this How to upload a file and JSON data in Postman?.

public async Task<IHttpActionResult> Post()
{
    var atorageAddress = HttpContext.Current.Request.Form["StorageAddress"].Split(',');
    var files = HttpContext.Current.Request.Files;
    List<HttpPostedFile> fileList = new List<HttpPostedFile>();
    foreach (var key in files.AllKeys)
    {
        // Do your logic here for manipulation of the files
        fileList.Add(files[key]);
    }

    return Json("OK");
}

UPDATE: Retrieve JSON

  1. Set your JSON like this.

    [{"Address":"Test Store 1"},{"Address":"Test Store 2"}]

  2. Retrieve it like this.

    var storageAddress = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(HttpContext.Current.Request.Form["StorageAddress"]);

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18
  • but i need to read it as json format and i cannot user second option because i need to pass image also using second option i cant pass image – chethu Sep 05 '20 at 08:40
  • @ tontonsevilla yes i know this method but i can't pass it from raw data because i also need to pass images from formdata in that both will not work when i send request – chethu Sep 05 '20 at 09:36
  • @chethu, how about option 5. – tontonsevilla Sep 05 '20 at 09:46
  • i am passing value as `{"Address":"Test Store 1"},{"Address":"Test Store 2"}` in postman how to read Address value of each in c# – chethu Sep 06 '20 at 06:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221033/discussion-between-tontonsevilla-and-chethu). – tontonsevilla Sep 06 '20 at 07:06
-1

You can add a parameters in your controller which handled a csv param like a StorageAddresses (ex http://localhost:30601/api/licence/seeds/application/finalsubmit?StorageAddresses=test%20score%201,test%20score%202)

You can follow this tutorial to implement it in your site https://www.strathweb.com/2017/07/customizing-query-string-parameter-binding-in-asp-net-core-mvc/

Arai680
  • 9
  • 3
  • Consider rephrasing your Answer to make it sound less like a question. Please also show how the code in the link answer the Question. At present you're forcing a click-through and your Answer isn't self-contained. See ["Provide context for links"](https://stackoverflow.com/help/how-to-answer). – Scratte Sep 05 '20 at 11:48