【发布时间】:2014-05-10 23:50:36
【问题描述】:
我需要向我的 Web API 发送和接收 IList:
API方法:
public IList<int> GetKeywordIdsFromIds([FromUri]List<int> ids)
{
// this methods retuns null or an IList<int>
return _iKeywordService.GetKeywordIdsFromIKeywordIds(ids);
}
调用方法:
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;
}
}
我得到错误:
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)
编辑:
我不知道这是否有帮助,但我是从 IIS 日志文件中获得的:
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:
默认路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
【问题讨论】:
-
查看这篇文章,如何在代码中添加参数:stackoverflow.com/questions/217070/…
-
这是 ConsoleApp 和 MVC Web App 之间的调用,因此您可以在问题中看到传递信息。真不知道是不是他问的。抱歉...我认为问题在于调用“url + iKeywordIds”,因为如果我尝试使用 url +“/1”,它可以正常工作
-
您的 API 函数可能需要一个类似于 .../api/controllername?ids=1&ids=2&ids=3 的 URL。您需要将客户端上的 List
转换为函数期望的查询字符串 -
您编写一些代码来解析您的列表并生成所需的查询字符串。例如,stackoverflow.com/questions/17096201/…
-
这个参数叫ids,不是iKeywordIds。而且我也不相信 URL 的其余部分是正确的。浏览到您网站的根目录并使用内置的 web api 文档来获取正确的 URL。
标签: c# asp.net asp.net-web-api