0

I wrote the following get api Route in C# that looks like the following...

    [HttpGet]
    [Route("ReadAllAppBased")]
    public List<Feedback> ReadAllAppBased(IEnumerable<string> Apps, string CAT)
    { 
            return null;
    }

but when I send a HTTP Request in JS I am not able to see my Collection. Please see the following code for JS. following...

    axios.get('http://localhost:63464/api/FeedBack/ReadAllAppBased',
    {
        params: { Apps: ["TESTING 3", "TESTING 123"], CAT: "TESTING" }
    })
    .then(res => {
        return res.data;
    })
    .then(res => {
        console.table(res);
    });

Please know that I am hitting the call in C#. I am not able to see the data coming in for Apps. I am able to see data for CAT.

Would like to know how I can fix this as I need to be able to send a Collection of string that needs to be received in C# from JS.

UncleFifi
  • 825
  • 1
  • 6
  • 9
  • It needs to be in the format `{ [0].Apps: "TESTING 3", [1].Apps: "TESTING 123", CAT: "TESTING" }` –  Oct 27 '18 at 20:38

3 Answers3

1

Can you please try sending the following Url to your request in axios please...

axios.get("/api/FeedBack/ReadAllAppBased?Apps=TESTING%203&Apps=TESTING%20123&CAT=TESTING")
.then(res => {
    return res.data;
})
.then(res => {
    console.table(res);
});

For more information about binding parameters, see Pass an array of integers to ASP.NET Web API?

robsonrosa
  • 2,548
  • 5
  • 23
  • 31
UncleFifi
  • 825
  • 1
  • 6
  • 9
0

Try using JSON.stringify as follows:

var data= {};
data.Apps= ["TESTING 3", "TESTING 123"];
data.CAT= "TESTING";
var dataJson= JSON.stringify(data);

Then pass dataJson as parameter.

Edit: Forgot to consider server-side changes. Change IEnumerable<string> Apps parameter in your ReadAllAppBased to string dataJson and in your axios.get replace params part with params: { dataJson: dataJson }.

In your ReadAllAppBased method use Newtonsoft.Json as follows:

var params = JsonConvert.DeserializeObject<DataJson>(dataJson);

Where your DataJson class is as follows:

class DataJson{ public List<string> Apps {get; set;} public string CAT {get; set;} }

Now you params should contain Apps & CAT.

Hope helps.

Note: since You are sending a collection, you may better consider using axios.post method as you params may be too long.

Zhavat
  • 214
  • 1
  • 6
  • Thank you for your input but I am still not able to see the data. I am not able to get any variable from the call – UncleFifi Oct 27 '18 at 20:48
0

Was missing [From Route] prior to function parameters

    [HttpGet]
    [Route("ReadAllAppBased")]
    public List<Feedback> ReadAllAppBased([From Query]IEnumerable<string> Apps, [From Query] string CAT)
    { 
            return null;
    }

then on the JS side I would need to send what @robsonrosa has suggested.

UncleFifi
  • 825
  • 1
  • 6
  • 9