【发布时间】:2017-03-15 22:57:02
【问题描述】:
更新:对于有相同错误/行为的人,请参阅答案 stackoverflow.com/a/42813181/1155847 - 我自己的愚蠢错误,但错误消息 - 好吧 没有给出真正的错误消息实际上.. 这么说吧,行为很奇怪..至少,现在解决了。
原始问题/问题:
我正在学习使用 WebAPI 构建一个 REST-ful 服务,您可以在以下位置下载代码:https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData *~ 克隆时请参阅下面的注意。
当我使用查询字符串localhost:8088/api/camps/1?includeSpeakers=true 调用下面代码的 GET 时,我在返回参数时遇到问题 - 邮递员和浏览器都给我返回“未找到服务器”,浏览器甚至重定向到 @987654327 @... 对我来说奇怪的行为,因为其他请求,例如 localhost:8088/api/camps/1 和 localhost:8088/api/camps 都可以正常工作。
如果我调试并单步执行,我发现它运行良好并获取 QueryString,对数据库执行它,甚至点击 return Ok(camp); 行...但我从来没有得到任何返回?
提琴手:
- 提琴手说
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
请求计数:1 发送字节:408(标头:408;正文:0)字节 收到:717(标题:205;正文:512)
实际表现
注意:此请求在接收操作失败后重试。
ClientConnected:14:20:36.281 ClientBeginRequest:14:20:44.067 GotRequestHeaders:14:20:44.067 ClientDoneRequest:14:20:44.067 确定网关:0 毫秒 DNS 查找:0 毫秒 TCP/IP 连接:0 毫秒 HTTPS 握手:0ms 服务器连接:14:20:44.294 FiddlerBeginRequest:14:20:44.294 ServerGotRequest:14:20:44.294 ServerBeginResponse: 00:00:00.000 GotResponseHeaders: 00:00:00.000 服务器完成响应:14:20:44.537 客户端开始响应:14:20:44.537 ClientDoneResponse: 14:20:44.538
总耗时:0:00:00.470
响应字节(按内容类型) -------------- text/html: 512 ~headers~: 205
环境:
在 Windows 10、Visual Studio 2017、IIS Express、localhost 端口 8088 上运行,未配置 SSL。
控制器:
using Microsoft.AspNetCore.Mvc;
using MyCodeCamp.Data;
namespace MyCodeCamp.Controllers {
[Route("api/[controller]")]
public class CampsController : Controller {
private readonly ICampRepository _campRepository;
public CampsController(ICampRepository campRepository) {
_campRepository = campRepository;
}
[HttpGet]
public IActionResult Get() {
try {
var camps = _campRepository.GetAllCamps();
return Ok(camps);
} catch { }
return BadRequest();
}
[HttpGet("{id}")]
public IActionResult Get(int id, bool includeSpeakers = false) {
try {
var camp = includeSpeakers
? _campRepository.GetCampWithSpeakers(id)
: _campRepository.GetCamp(id);
if (camp == null) {
return NotFound($"Camp with id '{id}' was not found.");
}
return Ok(camp);
} catch { }
return BadRequest();
}
}
}
~注意 当从https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData 克隆时首先做一个dotnet restore 并在MyCodeCamp.Data 文件夹中确保执行dotnet ef database update 来初始化数据库,否则你将无法如果需要,可以运行示例。
【问题讨论】:
标签: c# asp.net .net-core asp.net-web-api-routing