1

I need to send and receive an IList to my Web API:

API Method:

public IList<int> GetKeywordIdsFromIds([FromUri]List<int> ids)
{
     // this methods retuns null or an IList<int>
     return _iKeywordService.GetKeywordIdsFromIKeywordIds(ids);
}

Call method:

public List<int> GetKeywordIdsFromIds(IList<int> iKeywordIds)
{
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(API_BASE_URL);
            client.DefaultRequestHeaders.Accept.Clear();

            return client.GetAsync(url + iKeywordIds).Result.Content.ReadAsAsync<List<int>>().Result;
        }
}

I get the error:

Exception:Thrown: "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'text/html'." (System.Net.Http.UnsupportedMediaTypeException)

EDIT:

I don't know if this helps but I get this from IIS Log File:

2014-03-31 16:11:31 127.0.0.1 GET /api/ikeyword/getkeywordidsfromids/System.Collections.Generic.List`1[System.Int32] - 80 - 127.0.0.1 - 404 0 2 1

EDIT2:

Default route:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
17
  • See this post, how to add parameters to your code: stackoverflow.com/questions/217070/… Commented Mar 31, 2014 at 15:00
  • 1
    It's a call between a ConsoleApp and a MVC Web App so the information is passed as you can see in the question. I really don't know if it's what he is asking. Sorry...I think the problem is in the call "url + iKeywordIds" because if I try with url + "/1" it works fine Commented Mar 31, 2014 at 15:53
  • 1
    Your API function is probably expecting a URL that that looks something like .../api/controllername?ids=1&ids=2&ids=3. You need to convert your List<int> on the client to the querystring that the function is expecting Commented Mar 31, 2014 at 16:42
  • 1
    You write some code that parses your list and generates the required querystring. For example, stackoverflow.com/questions/17096201/… Commented Mar 31, 2014 at 16:48
  • 1
    The parameter is called ids, not iKeywordIds. And I'm not convinced that the rest of the URL is correct either. Browse to the root of your site and use the built-in web api documentation to get the right URL. Commented Mar 31, 2014 at 16:49

1 Answer 1

3

To call the Web API action with a list of ints in the query string, do it like this...

/api/ikeyword/getkeywordidsfromids?ids=1&ids=3&ids=4

or

/api/ikeyword/getkeywordidsfromids?ids[]=1&ids[]=3&ids[]=4

From your IIS logs, it looks like you need to properly format the list in your console app and append it to the URL. Your calling code should look something like this...

var parameters = ids.Select(id => string.Format("ids={0}", id)).ToArray();
var url = string.Format("/api/ikeyword/getkeywordidsfromids?{0}", string.Join("&", parameters));
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks, and how to I process the value in the WebApi method?
ids should be a List of ints. You can pass it straight to _iKeywordService.GetKeywordIdsFromIKeywordIds(ids)...
It's working, please just correct your code so I can set your answer as correct: remove the "=" in the Format method: getkeywordidsfromids?{0} or else in the Api it will not receive the first value
It's me who Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.