我得到了," System.Net.WebException。远程服务器返回错误:(500)内部服务器错误"并且,经过大量咬牙切齿和褶皱的脸颊(我永远不会告诉哪一对),通过在浏览器中输入我的Web API字符串,我发现真正的问题是:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Multiple actions were found that match the request: Int32 GetCountOfDepartmentRecords() on type HandheldServer.Controllers.DepartmentsController System.Collections.Generic.IEnumerable`1[HandheldServer.Models.Department] GetAllDepartments() on type HandheldServer.Controllers.DepartmentsController
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
</Error>
我看到我的控制器确实有两个无参数的方法:
public int GetCountOfDepartmentRecords()
{
return deptsRepository.Get();
}
public IEnumerable<Department> GetAllDepartments()
{
return deptsRepository.GetAll();
}
...但是一个返回一个int,另一个返回一个泛型集合;这是存储库代码:
public int Get()
{
return departments.Count;
}
public IEnumerable<Department> GetAll()
{
return departments;
}
...为什么Web API路由引擎会混淆?难道不够聪明地查看返回类型并选择正确的方法吗?
无论如何,我通过注释掉GetAll()方法解决了这个问题。